本文介绍了动态路径WPF的风格触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的样式,是有办法使绑定路径通用的,所以这种风格可以由多个用户使用,每个提供不同的绑定路径?
<风格X:键=OptionalBackground
的TargetType ={X:类型数据presenter:CellValue presenter}
支持算法FMP ={StaticResource的OptionalFieldCell presenter}>
< Style.Triggers>
< DataTrigger绑定={绑定的RelativeSource = {自我的RelativeSource},
路径= Record.DataItem.IsEditAllowed}
值=FALSE>
< setter属性=背景VALUE ={StaticResource的ReadOnlyField}/>
< / DataTrigger>
< /Style.Triggers>
< /样式和GT;
解决方案
您可以从风格
派生创造一个速记符号,看起来像这样:
<局部:BackgroundStyle
X:键=OptionalBackground
的TargetType ={X:类型数据presenter:CellValue presenter}
支持算法FMP ={StaticResource的OptionalFieldCell presenter}
PATH =Record.DataItem.IsEditAllowed
VALUE ={StaticResource的ReadOnlyField}/>
和用于本实施例的实施方案可能是:
公共类BackgroundStyle:样式,ISupportInitialize接口
{
公共字符串路径{搞定;组; }
公共对象值{搞定;组; } 公共无效BeginInit在(){} 公共无效EndInit在()
{
VAR触发=新DataTrigger
{
绑定=新绑定
{
路径=新的PropertyPath(路径)
的RelativeSource =新的RelativeSource(RelativeSourceMode.Self)
},
};
trigger.Setters.Add(新二传手(Control.BackgroundProperty,值));
Triggers.Add(触发);
}
}
In the following style, is there a way to make the Binding Path generic so that this style can be used by multiple consumers, each supplying a different binding path?
<Style x:Key="OptionalBackground"
TargetType="{x:Type DataPresenter:CellValuePresenter}"
BasedOn="{StaticResource OptionalFieldCellPresenter}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},
Path=Record.DataItem.IsEditAllowed}"
Value="False">
<Setter Property="Background" Value="{StaticResource ReadOnlyField}" />
</DataTrigger>
</Style.Triggers>
</Style>
解决方案
You can derive from Style
to create a shorthand notation that looks like this:
<local:BackgroundStyle
x:Key="OptionalBackground"
TargetType="{x:Type DataPresenter:CellValuePresenter}"
BasedOn="{StaticResource OptionalFieldCellPresenter}"
Path="Record.DataItem.IsEditAllowed"
Value="{StaticResource ReadOnlyField}"/>
and an implementation for this example might be:
public class BackgroundStyle : Style, ISupportInitialize
{
public string Path { get; set; }
public object Value { get; set; }
public void BeginInit() { }
public void EndInit()
{
var trigger = new DataTrigger
{
Binding = new Binding
{
Path = new PropertyPath(Path),
RelativeSource = new RelativeSource(RelativeSourceMode.Self)
},
};
trigger.Setters.Add(new Setter(Control.BackgroundProperty, Value));
Triggers.Add(trigger);
}
}
这篇关于动态路径WPF的风格触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!