WPF样式的DataTrigger绑定

编程入门 行业动态 更新时间:2024-10-18 06:01:48
本文介绍了WPF样式的DataTrigger绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在WPF中具有以下按钮和样式,并且需要在DataTrigger部分中概括绑定,因为我在同一Window中有近10个类似的按钮,并且每个按钮都应绑定到不同的属性(SelectedPositions,SelectedAgencies 、.). ...).有可能实现吗?

I have the following Button and Style in WPF and I need to generalize the Binding in the DataTrigger section because I have near 10 similar buttons in the same Window and each button should be binded to a different property (SelectedPositions, SelectedAgencies, ....). Is it possible to implement?

<Button x:Name="btnPosition" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Command="{Binding PositionFilterCommand}" Content="{l:Translate position}" Style="{StaticResource NewButtonStyle}" /> <Style x:Key="NewButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Foreground" Value="White" /> <Setter Property="Height" Value="22" /> <Setter Property="Width" Value="Auto" /> <Setter Property="FontFamily" Value="OpenSans" /> <Setter Property="FontSize" Value="13" /> <Setter Property="Cursor" Value="Hand" /> <Setter Property="Margin" Value="10,2,10,0" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border CornerRadius="3"> <Grid x:Name="gridButton" Background="#54728e"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image x:Name="img" Grid.Column="0" Width="24" Height="24" Source="Img/tick-white.png" Visibility="Visible" /> <Rectangle x:Name="rect" Grid.Column="1" Fill="#54728e" RadiusX="3" RadiusY="3" /> <ContentPresenter Grid.Column="1" Margin="5,0,5,0" HorizontalAlignment="Stretch" VerticalAlignment="Center" /> </Grid> </Border> <ControlTemplate.Triggers> <DataTrigger Binding="{Binding SelectedPositions}" Value="{x:Static sys:String.Empty}"> <Setter TargetName="rect" Property="Fill" Value="#8bbcdf" /> <Setter TargetName="img" Property="Visibility" Value="Collapsed" /> <Setter TargetName="gridButton" Property="Background" Value="#8bbcdf" /> </DataTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>

推荐答案

您能给我一个您所解释的例子吗?

could you provide me an example of what you explained?

好的,

1-使用标记

在您的Style中将您的DataTrigger设置为:

In your Style have your DataTrigger as:

<DataTrigger Binding="{Binding Path=Tag, RelativeSource={RelativeSource Self}}" Value="{x:Static sys:String.Empty}"> ... </DataTrigger>

用法:

<Button x:Name="btnPosition" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Command="{Binding PositionFilterCommand}" Content="{l:Translate position}" Tag="{Binding SelectedPositions}" Style="{StaticResource NewButtonStyle}" />

2-使用附加属性:

"local:"是指应用程序的xaml名称空间别名,或者,如果您使用其他名称空间,则声明MyCustomPropertyCollection的名称空间.

"local:" refers to the xaml namespace alias of your application or if you use different namespaces, the namespace where MyCustomPropertyCollection is declared.

隐藏代码:

public class MyCustomPropertyCollection { public static readonly DependencyProperty SomeStringProperty = DependencyProperty.RegisterAttached( "SomeString", typeof(string), typeof(MyCustomPropertyCollection), new FrameworkPropertyMetadata(string.Empty)); public static void SetSomeString(UIElement element, string value) { element.SetValue(SomeStringProperty, value); } public static string GetSomeString(UIElement element) { return (string)element.GetValue(SomeStringProperty); } }

Style.DataTrigger

<DataTrigger Binding="{Binding Path=(local:MyCustomPropertyCollection.SomeString), RelativeSource={RelativeSource Self}}" Value="{x:Static sys:String.Empty}"> ... </DataTrigger>

用法:

<Button x:Name="btnPosition" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Command="{Binding PositionFilterCommand}" Content="{l:Translate position}" local:MyCustomPropertyCollection.SomeString="{Binding SelectedPositions}" Style="{StaticResource NewButtonStyle}" />

3-常规依赖项属性

自定义Button类:

public class MyButton : Button { public static readonly DependencyProperty SomeStringProperty = DependencyProperty.Register( "SomeString", typeof(string), typeof(MyButton), new FrameworkPropertyMetadata(string.Empty)); public string SomeString { get { return (string)GetValue(SomeStringProperty); } set { SetValue(SomeStringProperty, value); } } }

xaml中的样式不仅需要更新DataTrigger,而且还需要Style定义.

Style in xaml not only needs DataTrigger updated but Style definition too.

所以切换

<Style x:Key="NewButtonStyle" TargetType="{x:Type Button}">

<Style x:Key="NewButtonStyle" TargetType="{x:Type local:MyButton}">

Style.DataTrigger

<DataTrigger Binding="{Binding Path=SomeString, RelativeSource={RelativeSource Self}}" Value="{x:Static sys:String.Empty}"> ... </DataTrigger>

用法:

<local:MyButton x:Name="btnPosition" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center" Command="{Binding PositionFilterCommand}" Content="{l:Translate position}" SomeString="{Binding SelectedPositions}" Style="{StaticResource NewButtonStyle}" />

Tag方法不被接受. 附加属性"更易于实现,但与具有常规DP和AP的自定义类一样,它也没有明显的依赖关系指标,因此也会被过度使用.选择想要的东西.

Tag approach is frowned upon. "Attached Property" is easier to implement but isn't as clear of a indicator of dependencies as a custom class with a normal DP and AP also gets way overused. Take your pick for what you'd prefer.

更多推荐

WPF样式的DataTrigger绑定

本文发布于:2023-11-06 23:43:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564993.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   样式   WPF   DataTrigger

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!