在代码中使用XAML资源字典

3
那是资源字典文件: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>

侧面 C#

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

当objectTree是TreeView时

然而那并不起作用。

我尝试了以下方法,它是可行的,但我需要重新定义 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
);

此外,我已尝试将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>

有人能帮我找到一种简单的方法在C#中使用它吗?


你说文件名是 TopologyTreeDisplay.xaml,但你试图加载没有 display 部分的 TopologyTree.xaml 文件。 - Blachshma
很抱歉,这是 TopologyTree.xaml 的内容,我在帖子中犯了一个错误。 - Enzojz
2个回答

3

您好,让我给您展示如何进行操作。 在此输入图片描述

<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) });

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

你好,但我尝试使用一个样式 objectTree.ItemContainerStyle = resourceDictionary["TopologyTreeStyle"] as Style,这个方法是有效的。 - Enzojz
我的观点是为你提供如何做到它的想法,其余的就全取决于你自己了。 - yo chauhan
抱歉,我发现我尝试的是使用<< objectTree.ItemContainerStyle = resourceDictionary["TopologyTreeStyle"] as Style >>,你的代码不起作用... - Enzojz
你好,好的,最终你的代码是正确的,但是重点是要从XAML代码中删除x:Key。谢谢。 - Enzojz

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接