簡體   English   中英

如何使用綁定綁定resourcedirectory源屬性

[英]how to bind resourcedirectory source property using binding

腳本

我在window.xaml有以下代碼window.xaml

<Window x:Class="MD.UI.EntryPoint.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:UICore="http://schemas.MasterData.io/Core/"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MD.UI.Core;component/Themes/Generic.xaml"></ResourceDictionary>
                <!--<ResourceDictionary Source="{DynamicResource }"></ResourceDictionary>-->
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Button Name="btnShow" Content="Show" HorizontalAlignment="Left" Margin="329,172,0,0" VerticalAlignment="Top" Width="83" Height="22" Click="btnShow_Click"/>
    </Grid>
</Window>

如果您熟悉XAML比你能明白Generic.xaml在一個單獨的項目定義( dll )名稱MD.UI.Core在這個項目中引用,這一切都工作正常 意味着Generic.xaml主題正常工作。

問題

MD.UI.Core也有http://schemas.MasterData.io/Core/ XmlnsDefinition這里使用:

 xmlns:UICore="http://schemas.MasterData.io/Core/"

所以,雖然我已經在window.xaml定義了xmlns:UICore ,但我不想再使用/MD.UI.Core;component/Themes/Generic.xaml了。 我想要做的是,通過使用xmlns我想綁定ResourceDirectory.Source屬性,如:

<ResourceDictionary Source="{DynamicResource }"></ResourceDictionary>

編輯

其實我想直接使用UICore引用之類的

<ResourceDictionary Source="{DynamicResource UICore:component/Themes/Generic.xaml}"></ResourceDictionary>

something東西,但使用XAML markup Binding表達式。

我不知道。 如何定義以上?
你能幫我么?

我的理解是,您要做的是能夠動態選擇主題(資源字典)。 我通常在C#中這樣做。 例如,假設我們有2個主題。 第一個嵌入在當前組件中,第二個嵌入在外部組件中。 讓我們把他們Internal.xamlExternal.xamlExternal.dll。 現在我們可以編寫以下方法:

private static void SetStyle(bool useInternal)
{
    var res = Application.Current.Resources;
    res.MergedDictionaries.Clear();

    if (useInternal)
        res.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("pack://application:,,,/Internal.xaml")
        });
    else
        res.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("pack://application:,,,/External;component/External.xaml")
        });
}

此方法已簡化,因為假設MergedDictionaries集合中只能有一個項目。 但是,它顯示了一個想法。 實際上,您可以相應地向MergedDictionaries集合添加/刪除資源。

值得指出的是,上面的SetStyle方法使用全局資源字典。 如果需要,您可以對本地詞典執行相同操作,例如對於特定窗口甚至控件。

暫無
暫無

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

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