相关疑难解决方法(0)

从父控件设置WPF嵌套控件属性

我有一个WPF窗口,上面有多个ListBox控件,所有这些都共享了我在这里简化的样式:

   <Style x:Key="listBox" TargetType="{x:Type ListBox}">
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border BorderBrush="Black">
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Text="{Binding Path=name}" />
                            <TextBlock Text="{Binding Path=text}" />
                            <TextBlock Text="id:" />
                            <TextBlock x:Name="_idTextBlock" Text="{Binding Path=id}" />
                            <Button Name="btnGet" CommandParameter="{Binding Path=id}" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
Run Code Online (Sandbox Code Playgroud)

以下是使用该样式的ListBox控件之一的示例:

<ListBox x:Name="lbCampaigns" Button.Click="lbCampaigns_Click" ItemsSource="{Binding}" Style="{StaticResource listBox}" />
Run Code Online (Sandbox Code Playgroud)

如何在父ListBox中设置Button控件的内容(btnGet)?

我知道我希望按钮在设计时为Window上的每个ListBox显示什么文本.(即我不需要将它绑定到ListBox ItemsSource).我看到我可以定义子控件的事件(请参阅Button.Click定义),但似乎我不能以相同的方式设置子控件的属性.

有任何想法吗?谢谢!

.net wpf xaml

4
推荐指数
1
解决办法
9054
查看次数

标签 统计

.net ×1

wpf ×1

xaml ×1