简体   繁体   English

有没有办法在 ResourceDictionary 中定义的样式中指定数据类型?

[英]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.

相关问题 在ResourceDictionary 中定义一个DataTrigger 的通用Style 但在视图中指定DataTrigger 绑定路径 - Defining a general Style with a DataTrigger in ResourceDictionary but specify the DataTrigger binding path in the view 在resourcedictionary中定义的样式不能正确设置TextBox VerticalContentAlignment - Style defined in resourcedictionary doesn't set TextBox VerticalContentAlignment properly 如何创建仅存在于ResourceDictionary上下文中的Style - How to make a Style that only exists within the context of a ResourceDictionary ResourceDictionary样式错误:“类型必须派生自FrameworkElement或FrameworkContentElement - ResourceDictionary Style Error: "type must derive from FrameworkElement or FrameworkContentElement ResourceDictionary中的样式Xaml标签 - Style Xaml Label in ResourceDictionary 使用在资源字典中定义的控件 - using control defined in resourcedictionary WPF: InvalidCastException: Unable to cast object of type 'System.Windows.Style' to type 'System.Windows.ResourceDictionary' - WPF: InvalidCastException: Unable to cast object of type 'System.Windows.Style' to type 'System.Windows.ResourceDictionary' 在运行时更改ResourceDictionary的样式值 - Change Style Values of ResourceDictionary at RunTime 从代码更改样式(在ResourceDictionary中) - Change style (in ResourceDictionary) from code WPF样式库中的ResourceDictionary组织 - ResourceDictionary organization in WPF style library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM