3

that's resourcedictionay file: 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#

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

while objectTree is a 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.

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.

    <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?

4

2 回答 2

3

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.

于 2013-02-14T13:20:41.600 回答
0

Finally I've found the answer in this topic :

Adding to a TreeView a ResourceDictionary from another Assembly

That works.

于 2013-02-14T15:07:44.680 回答