有没有办法在WPF中隐藏组合框的项目? 在我的usercontrol中有一个ListBox,其复选框项绑定到ObservableCollection和带有组合框的数据网格。
<ListBox x:Name="AvailableAttributes" Grid.Row="0" Grid.Column="2" SelectionMode="Single" >
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=OneWay}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsSelected}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
...
<DataGrid Name="datagrid" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" />
<DataGridComboBoxColumn
SelectedValueBinding="{Binding CBID}"
DisplayMemberPath="Name"
SelectedValuePath="ID">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource"
Value="{Binding Path=CBItems, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource"
Value="{Binding Path=CBItems, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
我使用this solution来管理组合框项目,并添加了属性&#39; IsSelected&#39;
public class GridItem
{
public string Name { get; set; }
public int CBID { get; set; }
}
public class CBItem
{
public int ID { get; set; }
public string Name { get; set; }
public bool IsSelected { get; set; }
}
现在我想使用&#39; IsSelected&#39;隐藏/显示组合框中项目的属性。有人能告诉我怎样才能实现这个目标?
答案 0 :(得分:5)
非常简单:只需给组合框项目添加一个带有触发器的样式,该触发器根据ComboBoxItem.Visibility
IsSelected
ComboBoxItem
上DataContext
的值设置<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
<Setter Property="ItemsSource"
Value="{Binding Path=CBItems, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ComboBoxItem" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
</Style>
:
IsSelected
如果在网格中加载ComboBox后,您可能会在这些项目中更新CBItem
的值,则需要在//this will start at 0.5 and keep it half way (1 second) and then aniate the fade to 1
@keyframes fadein {
0% {
opacity: 0.5;
}
50% {
opacity: 0.5;
}
100% {
opacity: 1;
}
}
//you can do all sorts like blinking etc
@keyframes fadein {
0% {
opacity: 0.5;
}
30% {
opacity: 1;
}
50% {
opacity: 0.5;
}
100% {
opacity: 1;
}
}
上implement INotifyPropertyChanged以便UI将反映变化。
答案 1 :(得分:0)
如果您想显示特定属性并根据另一个属性值过滤项目,您应该一起使用 ItemTemplate
和 ItemContainerStyle
。
在此示例中,ItemSource
已设置为 ObservableCollection
类型属性,该属性是另一个组合框 ItemSource
<ComboBox x:Name="combo2" ItemsSource="{Binding SelectedItem.Devices,ElementName=combo1}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Style.Triggers>
<DataTrigger Binding="{Binding DeviceId}" Value="125">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>