ide*_*lix 2 wpf listbox datatemplate listboxitem
在WPF应用程序中,我使用在XAML中的以下DataTemplate中定义的ItemTemplate创建Listbox:
<DataTemplate x:Key="ListItemTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel>
<Button/>
<Button/>
<Button Name="btnRefresh" IsEnabled="false"/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
<TextBlock/>
</StackPanel>
<TextBox/>
</Grid>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
生成ListBox后,我需要在所有ListBoxItem上更改以下按钮IsEnabled propety为true: <Button Name="btnRefresh" IsEnabled="false"/>
问题:
我无法访问ListBoxItem,因此无法使用该按钮访问其子项.
在WPF中是否有像ListBox.Descendents()这样的Silverlight或任何其他方式来获取该按钮,
执行此操作的首选方法是更改ViewModel
绑定到Button的IsEnabled属性的属性.向ListBox.Loaded
事件添加处理程序,并在加载ListBox时将ViewModel中的该属性设置为false.
另一个选项是,如果需要遍历ListBox中的每个数据模板化项,请执行以下操作:
if (listBox.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
{
foreach (var item in listBox.Items)
{
ListBoxItem container = listBox.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
// Get button
ContentPresenter contentPresenter = contentPresenter.ContentTemplate.FindName("btnRefresh", contentPresenter);
Button btn = contentPresenter as Button;
if (btn != null)
btn.IsEnabled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8507 次 |
最近记录: |