gpt4 book ai didi

c# - 设置边框样式模板,但允许灵活使用值

转载 作者:太空宇宙 更新时间:2023-11-03 12:42:19 26 4
gpt4 key购买 nike

我有这个 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>

这个位:

Value="{x:Static StudentInfoEnums:StudentAssignmentType.Student1Main}">

需要为窗口上的每个 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>

但这行不通。

最佳答案

很漂亮,但可以增加您的可能性,因为您可以绑定(bind)您的StudentInfoEnums:StudentAssignmentType.Student1Main-Enum。

一些随机演示 - 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>

我的演示枚举:

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 类型的一切(因为控件有 BorderBrush-Property)
  • 我已经实现了一个 DependencyProperty 来使事物可绑定(bind)。
  • 四个你的目的,你一定要用你的替换 MyEnum 和调整颜色。此外,您可能必须实现另一个DependencyProperty 将您的第二个条件带到行为中。

希望这能为您提供线索,让您了解如何继续编写代码。

关于c# - 设置边框样式模板,但允许灵活使用值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38480443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com