[英]Is there a way to specify the data type within a style defined in a ResourceDictionary?
Consider the following xaml definition of a UserControl
:考虑以下 xaml 的
UserControl
定义:
<UserControl x:Class="Demo.MyControl" x:Name="self">
<Grid Binding="{Binding ElementName=self}">
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=MyBooleanProperty}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
</UserControl>
In the grid's binding above, I'm using ElementName=self
, which directs to the control itself.在上面的网格绑定中,我使用的是
ElementName=self
,它指向控件本身。 This is just an example of having a binding where the data type of the binding source used by the DataTrigger
is known at design-time.这只是具有绑定的示例,其中
DataTrigger
使用的绑定源的数据类型在设计时是已知的。 Another would be defining a style within a DataTemplate
that has a DataType
attribute.另一个是在具有
DataType
属性的DataTemplate
中定义样式。
I would like to extract styles defined locally to a ResourceDictionary
, but then the data type is not available at design-time and the xaml editor cannot supply the intellisense support I have, if the style is defined as above, eg for the Path
property.我想将本地定义的 styles 提取到
ResourceDictionary
,但是如果样式定义如上,例如对于Path
属性,则数据类型在设计时不可用,并且 xaml 编辑器无法提供我拥有的智能感知支持。
Is there a way to specify the data type within a style defined in a ResourceDictionary?有没有办法在 ResourceDictionary 中定义的样式中指定数据类型?
The only thing I can think of would be using the d:DataContext
property that could hint in the right direction, unfortunately my VS doesn't accept it on a style.我唯一能想到的就是使用
d:DataContext
属性,它可以向正确的方向暗示,不幸的是我的 VS 不接受它的样式。
Besides that, I don't think there is any meaningful way to solve this since a style can be applied to any element of the TargetType
and the designer doesn't know the elements it will be applied to.除此之外,我认为没有任何有意义的方法可以解决这个问题,因为样式可以应用于
TargetType
的任何元素,而设计人员不知道它将应用于哪些元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.