wpf - 通过绑定(bind)值选择控件样式

标签 wpf data-binding triggers

我有一个可用于多个编辑对象的编辑器 View 。多个对象的 View 模型为每个需要处理的字段提供了一个类似 Field1Multiple 的 bool 类型的属性。在本例中,目前只有 ComboBox 控件。每当应为该字段指示多个不同值时,应将特定样式应用于 App.xaml 中定义的该控件。该样式会更改控件的背景,以显示此处没有可显示的单个值。

我试过使用此 XAML 代码:

<ComboBox
  ItemsSource="{Binding Project.Field1Values}" DisplayMemberPath="DisplayName"
  SelectedItem="{Binding Field1}">
  <ComboBox.Style>
    <Style>
      <Style.Triggers>
        <DataTrigger Binding="{Binding Field1Multiple}" Value="true">
          <Setter
            Property="ComboBox.Style"
            Value="{StaticResource MultiValueCombo}"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ComboBox.Style>
</ComboBox>

但它不起作用,因为我无法从 Style 内部设置 Style 属性。编译器说,如果我直接在控件上使用触发器,可能只有 EventTriggers,没有 DataTriggers。

如何根据绑定(bind)值设置控件的样式?或者,如果绑定(bind)值为真,我如何为控件设置特定样式?

最佳答案

(编辑完整的解决方案)

你可以使用转换器:

  public class AnyIsMultipleToStyle : IValueConverter
    {
        public Style NormalStyle { get; set; }

        public Style MultiStyle { get; set; }


        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                IList<SampleClass> list= value as IList<SampleClass>;
                if (list!=null)
                {
                    if (list.Any(i => i.Multi))
                        return MultiStyle;

                }

            }
            return NormalStyle;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

并且在您的 xaml 中:(您向转换器指示普通样式和多样式)

 <Window.Resources>

        <Style x:Key="MultiValueCombo"  TargetType="{x:Type ComboBox}">

            <Setter Property="Background" Value="Olive" />
        </Style>

        <Style x:Key="NormalCombo"  TargetType="{x:Type ComboBox}">
            <Setter Property="Background" Value="Red" />
        </Style>
        <my:AnyIsMultipleToStyle x:Key="AnyIsMultipleToStyle1" MultiStyle="{StaticResource MultiValueCombo}" NormalStyle="{StaticResource NormalCombo }"  />





    </Window.Resources>
    <Grid>

        <ComboBox    ItemsSource="{Binding Items, ElementName=root}"  >
            <ComboBox.Style>
                <Binding Converter="{StaticResource AnyIsMultipleToStyle1}" Path="Items" ElementName="root" >

                    </Binding>
            </ComboBox.Style>

        </ComboBox>
    </Grid>

关于wpf - 通过绑定(bind)值选择控件样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12408670/

相关文章:

asp.net - 使用 VB.NET 在 ASP.NET 中的 gridview 中没有任何内容时隐藏 <TD>

wpf - 在 WPF 中使用数据绑定(bind)启动动画

mysql - 投标历史表触发器

sql - 在 PostgreSQL 中创建触发器时出现 NEW 语法错误

c# - Wpf - 单独程序集或文件夹中的 MVVM 和服务

c# - 当 ItemsSource 与 ResourceDictionaries 或 MergedDictionaries 一起使用 setter 属性时,MenuItem 样式

wpf - 寻找 WPF Grid ColumnSpan 行为的解释

Android数据绑定(bind),单选按钮不更新

sql - 如何使用带有触发器的子查询条件,如 "NOT IN"?

c# - UIElement 相对于 Window 的位置