英文:
How to display "No Items" content in ComboBox if target ItemsSource is null?
问题
我正在尝试创建一个具有可更新集合的组合框。然后,如果绑定到组合框的集合为空,我想在组合框内显示特定内容。
我已尝试设置TargetNullValue
参数,但它显示的内容与我期望的不同。
<ComboBox ItemsSource="{Binding MyCollection, UpdateSourceTrigger=PropertyChanged, TargetNullValue='没有项目'}"/>
我现在拥有的是:
我期望的是:
英文:
I am trying to create a Combobox with updatable collection. Than I would to display special content inside combobox if collection which binding to combobox in null.
I had try to set TargetNullValue
parameter but it's display content not as I expected.
<ComboBox ItemsSource="{Binding MyCollection, UpdateSourceTrigger=PropertyChanged, TargetNullValue='No Items'}"/>
What I Have Now
What I Expect
答案1
得分: 2
你可以在 ComboBox 没有项目时替换其模板,例如使用 TextBlock 或任何其他可视元素。
<ComboBox>
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="HasItems" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<TextBlock Text="No Items"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
英文:
You may replace the ComboBox's Template when it has no items, e.g. with a TextBlock or any other visual element.
<ComboBox>
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="HasItems" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<TextBlock Text="No Items"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
答案2
得分: 0
尝试这个(我无法自行检查它 - 我手边没有计算机):
<ComboBox>
<ComboBox.Resources>
<CompositeCollection x:Key="noItems">
<sys:String>无项目</sys:String>
</CompositeCollection>
</ComboBox.Resources>
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding MyCollection}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding MyCollection.Count}"
Value="0">
<Setter Property="ItemsSource" Value="{DynamicResource noItems}"/>
<Setter Property="SelectedIndex" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
或者对于TargetNullValue的变种:
<ComboBox>
<ComboBox.ItemsSource>
<Binding Path="MyCollection">
<Binding.TargetNullValue>
<CompositeCollection>
<sys:String>无项目</sys:String>
</CompositeCollection>
</Binding.TargetNullValue>
</Binding>
</ComboBox.ItemsSource>
</ComboBox>
英文:
Try this (I can’t check it myself - I don’t have a computer “at hand”):
<ComboBox>
<ComboBox.Resources>
<CompositeCollection x:Key="noItems">
<sys:String>No Items</sys:String>
</CompositeCollection>
</ComboBox.Resources>
<ComboBox.Style>
<Style TergetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding MyCollection}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding MyCollection.Count}"
Value="0">
<Setter Property="ItemsSource" Value="{DynamicResource noItems}"/>
<Setter Property="SelectedIndex" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
<ComboBox.Style>
</ComboBox>
Or such variant for TargetNullValue:
<ComboBox>
<ComboBox.ItemsSource>
<Binding Path="MyCollection">
<Binding.TargetNullValue>
<CompositeCollection>
<sys:String>No Items</sys:String>
</CompositeCollection>
</Binding.TargetNullValue>
</Binding>
</ComboBox.ItemsSource>
</ComboBox>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论