组合框ItemTemplate和前景 [英] Combobox ItemTemplate and foreground
问题描述
< ComboBox x:Name =myComboBoxItemsSource ={Binding Names}>
< ComboBox.ItemTemplate>
< DataTemplate>
< TextBlock Text ={Binding ...}>
< TextBlock.Style>
< Style TargetType ={x:Type TextBlock}>
< Style.Triggers>
< DataTrigger Binding ={Binding IsActive}Value =True>
< Setter Property =ForegroundValue =Navy/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /TextBlock.Style>
< / TextBlock>
< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>
我不知道如何绑定TextBlock的文本。我想要的是显示字符串列表。我总是得到一个具有项目但不可见的下拉列表。我尝试删除可能我在那里修剪的风格触发器思维,但这没有帮助。
我采取正确的方法吗?在搜索IsActive或者绑定不正确时,ComboBox.ItemTemplate会正确地查看我的viewModel(这是数据上下文)吗?
每个 ComboBoxItem
的 DataContext
是一个字符串,所以
- 对于
TextBlock
,绑定到DataContext
像Text ={Binding}
-
对于
DataTrigger
可以找到IsActive
,在绑定中使用RelativeSource
< ComboBox x:Name =myComboBoxItemsSource ={Binding Names}>
< ComboBox.ItemTemplate>
< DataTemplate>
< ; TextBlock Text ={Binding}>
< TextBlock.Style>
< Style TargetType ={x:Type TextBlock}>
< Style.Triggers>
< DataTrigger Bindi ng ={Binding RelativeSource = {RelativeSource AncestorType = {x:Type ComboBox}},
Path = DataContext.IsActive}
Value =True>
< Setter Property =ForegroundValue =Navy/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /TextBlock.Style>
< / TextBlock>
< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>
I have a comboBox that is bound to a list of strings from my viewModel. What I am trying to do is have the foreground of a comboBox item be set to a different color if a property in my viewModel is true:
<ComboBox x:Name="myComboBox" ItemsSource="{Binding Names}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ...}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsActive}" Value="True">
<Setter Property="Foreground" Value="Navy"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I am not sure what to bind the Text of the TextBlock to. All I want is to display the list of strings. I always end up with a dropdown that has the items but they are not visible. I tried removing the style trigger thinking that maybe I was screwing up there, but that didn't help.
Am I taking the right approach? Will the ComboBox.ItemTemplate correctly look at my viewModel (which is the data context) when searching for IsActive or is that binding incorrect as well?
The DataContext
for each ComboBoxItem
is a string so
- For the
TextBlock
, bind to theDataContext
likeText="{Binding}
For the
DataTrigger
to be able to findIsActive
, useRelativeSource
in the binding<ComboBox x:Name="myComboBox" ItemsSource="{Binding Names}"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding}"> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}, Path=DataContext.IsActive}" Value="True"> <Setter Property="Foreground" Value="Navy"/> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>
这篇关于组合框ItemTemplate和前景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!