wpf 在代码中使用 XAML resourceDictionary

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14874518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:31:27  来源:igfitidea点击:

Using XAML resourceDictionary in code

c#wpfxamlresourcedictionary

提问by Enzojz

that's resourcedictionay file: TopologyTree.xaml

那是资源字典文件:TopologyTree.xaml

<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:autoDraw.ViewModel.Topology"
>

<HierarchicalDataTemplate x:Key="TopologyTreeBase" DataType="{x:Type local:Base}" ItemsSource="{Binding children}">
    <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
        <TextBlock Text="{Binding name}"></TextBlock>
    </StackPanel>
</HierarchicalDataTemplate>

</ResourceDictionary>

Side C#

侧面 C#

objectTree.Resources = new ResourceDictionary();
objectTree.Resources.Source = new Uri("GUI/TopologyTree.xaml", UriKind.Relative);

while objectTree is a TreeView

而 objectTree 是一个 TreeView

how ever that doesn't work.

这怎么行不通。

I have tried as followed which worked, but I need to redefine DataType here, so I think it's not so good.

我试过如下,效果很好,但我需要在这里重新定义DataType,所以我认为它不是那么好。

var resourceDictionary = new ResourceDictionary();
resourceDictionary.Source = new Uri("GUI/TopologyTree.xaml", UriKind.Relative);

objectTree.Resources.Add(
    new DataTemplateKey(typeof(ViewModel.Topology.Base)),
    resourceDictionary["TopologyTreeBase"] as HierarchicalDataTemplate
);

What's more, I have tried put the content of xaml into the xmal window directly as followed, That works, but I need it be loaded dyanmically, so it just proven that my xmal is good.

更重要的是,我已经尝试将xaml的内容直接放入xmal窗口,如下所示,这有效,但我需要动态加载它,所以它证明了我的xmal是好的。

    <TreeView Name="objectTree" Grid.Column="4" Margin="3" Grid.Row="1" Grid.RowSpan="3">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Topology.Base}" ItemsSource="{Binding children}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
                    <TextBlock Text="{Binding name}"></TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>

Could anyone help me have a way to use it in C# side simply?

谁能帮我简单地在 C# 端使用它?

采纳答案by yo chauhan

Hi let me show you example how to do it enter image description here

嗨,让我向您展示如何操作的示例 在此处输入图片说明

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type TextBox}">
    <Setter Property="Foreground" Value="Red"/>
</Style>

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:uc="clr-namespace:WpfApplication1"
    xmlns:local="clr-namespace:WpfApplication1"
    Width="1000" Height="1000"
    Title="MainWindow"   x:Name="abc">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions> 
    <TextBox x:Name="tbx"/>
</Grid>

tbx.Resources.MergedDictionaries.Add(
         new ResourceDictionary { Source = new Uri(@"\Resources\MyResources.xaml", UriKind.Relative) });

Don't assign ResourceDictionary to Source , just add it to the Collection of MergedDictionary.

不要将 ResourceDictionary 分配给 Source ,只需将其添加到 MergedDictionary 的集合中。

回答by Enzojz

Finally I've found the answer in this topic :

最后我在这个话题中找到了答案:

Adding to a TreeView a ResourceDictionary from another Assembly

将另一个程序集中的 ResourceDictionary 添加到 TreeView

That works.

那个有效。