我有一个ListBox
,其ItemsSource
绑定到名为Employees
类的Employee
的对象列表。在ItemTemplat
e内部,我已经定义了DataTemplate
,并且希望列表中的每个项目都像EmployeeName AnIcon
一样显示。
下面是已经显示它的代码。请注意,TextBlock
已绑定到Name
类中的变量之一Employee
。同样,内部按钮的IsCreatedAtOffice
变量也是Employee class
中的变量之一。一切都按预期进行。
<ListBox x:Name="listbox"
ItemsSource="{Binding Employees}"
........
SelectedItem="{Binding CurrentEmployee}"
ItemContainerStyle="{StaticResource defaultListBoxItem}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="64">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="64"/>
</Grid.ColumnDefinitions>
<TextBlock
Grid.Column="0"
Text="{Binding Name}" />
<DockPanel Grid.Column="1" Visibility="{Binding ElementName=root, Path=DataContext.IsNotEditable, Converter={StaticResource BoolToVisibilityConverter}}">
<Button Visibility="{Binding IsCreatedAtOffice, Converter={StaticResource BoolToVisibilityConverterNotReversed}}" >
<Image Grid.Column="1" Width="48" Source="{Binding IsCreatedAtOffice, Converter={StaticResource EmpSyncStatus}}" />
</Button>
</DockPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
但是现在,我要实现的是:
基于在其MVVM视图模型类中设置的布尔变量切换完整Visibility
的{{1}}。该变量不在DockPanel
类内。可以说Employee
。
如果vm类中的IsFeatureSupported
是IsFeatureSupported
,那么我希望此true
(按钮和图标)可见,否则我想将其隐藏。
基本上,我不知道如何将DockPanel
的{{1}}绑定与现有元素中的根元素的Visibility
分离(如代码所示)并将其附加到{{1} }不属于DockPanel
的一部分?