Wpf 列表框在运行时从代码隐藏修改项的datatemplate
我有这个列表框Wpf 列表框在运行时从代码隐藏修改项的datatemplate,wpf,xaml,listbox,datatemplate,Wpf,Xaml,Listbox,Datatemplate,我有这个列表框 <ListBox x:Name="lbsample" ItemsSource="{Binding CurrentSettings}" SelectionMode="Extended"> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem" > <
<ListBox x:Name="lbsample" ItemsSource="{Binding CurrentSettings}" SelectionMode="Extended">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" >
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<GroupBox x:Name="gb0" Margin="0" Background="Transparent">
<GroupBox.Header>
<CheckBox x:Name="chk0" Content="xxx"
IsChecked="{Binding Path=DataContext.IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" />
</GroupBox.Header>
<!---->
<StackPanel Orientation="Horizontal" IsEnabled="{Binding Path=IsEnabled}" >
<TextBlock x:Name="lbltxt" Text="yyy"
HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5,5,5,10"/>
</StackPanel>
</GroupBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
加载掩码时,我希望以编程方式从代码隐藏更改文本“xxx”和“yyy”
我该怎么做?我无法访问属性,因为listbox已绑定且具有itemsource
我知道这不是最好的做法。。但我没有简单的选择。。
谢谢 如果需要特定于视图的内容,请添加加载的事件处理程序:
<CheckBox x:Name="chk0" Content="xxx" Loaded="CheckBoxLoaded" ...>
但是,您可以在item类和简单绑定复选框中声明特殊属性:
<CheckBox x:Name="chk0" Content="{Binding PropertyX}" ...>
同样的方法也适用于TextBlock。注意,当您在数据模板中命名元素时,您正在创建内存泄漏!完美的正是我想要的谢谢!
<CheckBox x:Name="chk0" Content="{Binding PropertyX}" ...>