【问题标题】:How to make the style setter data property dynamically in wpf (style is not in the main window xaml it is separate xaml)如何在wpf中动态制作样式设置器数据属性(样式不在主窗口xaml中,它是单独的xaml)
【发布时间】:2015-02-12 09:32:49
【问题描述】:

我如何在 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>

此代码在 DrawingStencils.xaml 页面中 mainwindow.xmal.cs页面如何动态设置数据?

【问题讨论】:

    标签: c# wpf dynamic styles setter


    【解决方案1】:

    如果我理解这个问题,我相信您是在问如何使用另一个 XAML 文件中的一个 XAML 文件中定义的 WPF 样式。

    您需要做的是引用 MainWindow.xaml 文件中的 XAML 样式文件 (DrawingStencils.xaml) 作为 ResourceDictionary,然后使用 StaticResource 关键字将该样式应用于目标类型。

    您确定要将此样式应用于类型路径吗?

    下图是一个简单的 Button 样式,定义为单独的文件 (Styles.xaml),然后在 MainView.xaml 文件中引用:

    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 文件:

    <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>
    

    【讨论】:

    • 感谢您的回答,现在我该如何指定路径数据,即() 来自后面的代码
    • 你可以从后面的代码中做到这一点,但你真的应该只想在 XAML 中做到这一点
    • 为什么会这样?那么如何动态更改数据(这里是形状),数据是绘制形状的坐标。
    • 如果你想改变 UI 中的形状,你会使用动画来做到这一点,这是如何触发的取决于你的要求,但你需要研究如何在 WPF 中的 XAML 中创建动画.
    猜你喜欢
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多