设置边框样式模板,但允许使用值的灵活性

时间:2016-07-20 11:47:01

标签: c# wpf xaml

我有Border风格:

<Border.Style>
    <Style x:Uid="Style_36" TargetType="Border">
        <Setter x:Uid="Setter_94" Property="BorderBrush" Value="Transparent"/>
        <Style.Triggers>
            <DataTrigger x:Uid="DataTrigger_36" Binding="{Binding SelectedItem, ElementName=comboActiveStudentAssignmentType}" 
                         Value="{x:Static StudentInfoEnums:StudentAssignmentType.Student1Main}">
                <Setter x:Uid="Setter_95" Property="BorderBrush" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Border.Style>

我知道如何将样式放入Window.Resources,然后将其应用于任何控件。但我需要调整每个实例。在文中:

<Border.Style>
    <Style x:Uid="Style_36" TargetType="Border">
        <Setter x:Uid="Setter_94" Property="BorderBrush" Value="Transparent"/>
        <Style.Triggers>
            <DataTrigger x:Uid="DataTrigger_36" Binding="{Binding SelectedItem, ElementName=comboActiveStudentAssignmentType}" 
                         Value="{x:Static StudentInfoEnums:StudentAssignmentType.Student1Main}">
                <Setter x:Uid="Setter_95" Property="BorderBrush" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Border.Style>

这一位:

  

值=&#34; {x:静态StudentInfoEnums:StudentAssignmentType。 Student1Main }&#34;&gt;

需要更改窗口中的每个Border。那么,我如何设置样式来简化我的代码但允许更改此属性?

可能的?

更新

由于每个边框只应在组合为特定值时显示,并且建议将所有数据触发器放入样式模板中,我首先尝试:

<Style x:Uid="Style_38" x:Key="StudentAssignmentFocusedBorder" TargetType="Border">
    <Setter x:Uid="Setter_94" Property="BorderBrush" Value="Transparent"/>
    <Style.Triggers>
        <MultiDataTrigger x:Uid="MultiDataTrigger_5">
            <MultiDataTrigger.Conditions>
                <Condition x:Uid="Condition_11" Binding="{Binding SelectedtItem, ElementName=comboActiveStudentAssignmentType}" Value="{x:Static StudentInfoEnums:StudentAssignmentType.Student1Main}"/>
                <Condition x:Uid="Condition_12" Binding="{Binding Name, Mode=OneWay, RelativeSource={RelativeSource Self}}" Value="borderMainHallStudent1"/>
            </MultiDataTrigger.Conditions>
            <Setter x:Uid="Setter_95" Property="BorderBrush" Value="Red"/>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

但这不起作用。

1 个答案:

答案 0 :(得分:1)

漂亮,但增强了您的可能性,因为您可以绑定StudentInfoEnums:StudentAssignmentType.Student1Main - 枚举。

一些随机演示 - XAML 来测试:

<Window x:Class="SelectButtonSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SelectButtonSample"

        mc:Ignorable="d"        
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        Title="MainWindow" >
    <Grid Height="200">
        <StackPanel>
            <Button Content="Click me" Width="80" Height="20" >
                <i:Interaction.Behaviors>
                    <local:MyBorderBehavior MyEnumPropery="Two"/>
                </i:Interaction.Behaviors>
            </Button>
            <CheckBox Content="Click me" x:Name="chk">
                <i:Interaction.Behaviors>
                    <local:MyBorderBehavior MyEnumPropery="Three"/>
                </i:Interaction.Behaviors>
            </CheckBox>
            <ListView>
                <i:Interaction.Behaviors>
                    <local:MyBorderBehavior MyEnumPropery="One"></local:MyBorderBehavior>
                </i:Interaction.Behaviors>
              <ListViewItem Content="Item1">
                    <i:Interaction.Behaviors>
                        <local:MyBorderBehavior MyEnumPropery="Four"></local:MyBorderBehavior>
                    </i:Interaction.Behaviors>
                </ListViewItem>
                <ListViewItem>Item 2</ListViewItem>
                <ListViewItem>Item 3</ListViewItem>
                <ListViewItem>Item 4</ListViewItem>
            </ListView>

        </StackPanel>
    </Grid>
</Window>

My Demo-Enum:

public enum MyEnum
    {
        One,
        Two,
        Three,
        Four

    }

魔术

public class MyBorderBehavior : Behavior<Control>
    {
        public MyEnum MyEnumPropery {
            get { return (MyEnum) GetValue(MyEnumProperyProperty); }
            set { SetValue(MyEnumProperyProperty, value); }
        }

        public static readonly DependencyProperty MyEnumProperyProperty = DependencyProperty.Register("MyEnumPropery", typeof(MyEnum), typeof(MyBorderBehavior), new PropertyMetadata(PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject dO, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var self = dO as MyBorderBehavior;
            if (self != null && self._controlToColorBorder != null)
                self.SetColor();
        }

        private Control _controlToColorBorder;

        private void SetColor()
        {
            switch (this.MyEnumPropery)
            {
                case MyEnum.One:
                    this._controlToColorBorder.BorderBrush = Brushes.Yellow;
                    break;
                case MyEnum.Two:
                    this._controlToColorBorder.BorderBrush = Brushes.Red;
                    break;
                case MyEnum.Three:
                    this._controlToColorBorder.BorderBrush = Brushes.Green;
                    break;
                case MyEnum.Four:
                    this._controlToColorBorder.BorderBrush = Brushes.DeepPink;
                    break;
            }
        }

        protected override void OnAttached()
        {
            this._controlToColorBorder = this.AssociatedObject;
            this._controlToColorBorder.Loaded += ControlToColorBorderLoaded;
            base.OnAttached();
        }

        private void ControlToColorBorderLoaded(object sender, RoutedEventArgs e)
        {
            this.SetColor();
        }
    }

备注:

  • 如你所见,你必须使用 System.Windows.Interactivity - 装配
  • 这个小Behavior可以应用于Control类型的所有内容 (由于Control有BorderBrush - 属性)
  • 我已经实现了DependencyProperty以使事物可绑定。
  • 你的四个目的,你肯定要用你的MyEnum取而代之 调整颜色。此外,您可能必须实现另一个 DependencyProperty将您的第二个条件带入行为。

希望这能为您提供线索,了解如何开始使用代码。