簡體   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)

我如何在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頁面中動態設置數據?

如果我理解這個問題,我相信您正在詢問如何使用另一個XAML文件中的一個XAML文件中定義的WPF樣式。

您需要做的是將MainWindow.xaml文件中的XAML樣式文件(DrawingStencils.xaml)作為ResourceDictionary引用,然后使用StaticResource關鍵字將樣式應用於目標類型。

您確定要將此樣式應用於Path嗎?

下面顯示的是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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM