简体   繁体   English

如何在wpf中动态制作样式设置器数据属性(样式不在主窗口xaml中它是单独的xaml)

[英]How to make the style setter data property dynamically in wpf (style is not in the main window xaml it is separate xaml)

How di I make the style setter data property dynamically in wpf (style is not in the main window xaml it is separate xaml) 我如何在wpf中动态创建样式设置器数据属性(样式不在主窗口xaml中,它是单独的xaml)

 <Style x:Key="Room3HZ"
        TargetType="Path"
        BasedOn="{StaticResource DrawingItemStyle}">
        <Setter Property="Data"
                Value="M 17,70 L 550,70 551,331 17,331 17,70 195,70 195,330 367,330  367,69"/>
    </Style>

This code is in DrawingStencils.xaml page how to dynamically set the data in mainwindow.xmal.cs page? 这段代码在DrawingStencils.xaml页面中如何在mainwindow.xmal.cs页面中动态设置数据?

If I understand the question, I believe you are asking how to use a WPF Style defined in one XAML file from another XAML file. 如果我理解这个问题,我相信您正在询问如何使用另一个XAML文件中的一个XAML文件中定义的WPF样式。

What you need to do is reference the XAML style file (DrawingStencils.xaml) in the MainWindow.xaml file as a ResourceDictionary and then apply the style to the target type using the StaticResource keyword. 您需要做的是将MainWindow.xaml文件中的XAML样式文件(DrawingStencils.xaml)作为ResourceDictionary引用,然后使用StaticResource关键字将样式应用于目标类型。

Are you sure you want to apply this style to type Path? 您确定要将此样式应用于Path吗?

Shown below is a simple style for Button which is defined is separate file (Styles.xaml) and then referenced in the MainView.xaml file: 下面显示的是Button的简单样式,该样式定义为单独的文件(Styles.xaml),然后在MainView.xaml文件中进行引用:

Styles.xaml file: Styles.xaml文件:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="MyButtonStyle"
           TargetType="Button">
        <Setter Property="Background"
                Value="Red" />        
    </Style>

</ResourceDictionary>

MainView.xaml file: MainView.xaml文件:

<Window x:Class="StyleExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>

        <Button Content="My Button"
                Style="{StaticResource MyButtonStyle}" />

    </Grid>
</Window>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM