【问题标题】:Using XAML resourceDictionary in code在代码中使用 XAML 资源字典
【发布时间】:2013-02-14 23:07:44
【问题描述】:

这是资源字典文件: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 但您正在尝试加载 TopologyTree.xaml - 没有 display 部分
  • 抱歉,这是TopologyTree.xaml extact,我在帖子中出错了

标签: c# wpf xaml resourcedictionary


【解决方案1】:

您好,让我向您展示如何做到这一点的示例

<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"] 作为 Style,有一种风格,有效。
  • 我的意思是让您知道如何做到这一点。休息就靠你了。
  • 对不起,我发现我尝试的是 > ,你的代码不起作用......
  • 你好,最后你的代码是正确的,但关键是从 XAML 代码中删除 x:Key。谢谢。
【解决方案2】:

我终于在这个话题中找到了答案:

Adding to a TreeView a ResourceDictionary from another Assembly

这行得通。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多