组合框ItemTemplate和前景 [英] Combobox ItemTemplate and foreground

查看:115
本文介绍了组合框ItemTemplate和前景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从我的viewModel绑定到一个字符串列表的组合框。我想要做的是将comboBox项目的前景设置为不同的颜色,如果我的viewModel中的属性为true:

 < 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 the DataContext like Text="{Binding}
  • For the DataTrigger to be able to find IsActive, use RelativeSource 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆