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?

2
  • You say the name of the file is TopologyTreeDisplay.xaml yet you're trying to load TopologyTree.xaml - without the display part Commented Feb 14, 2013 at 13:11
  • sorrsy, that's TopologyTree.xaml extact, i made an error in the post Commented Feb 14, 2013 at 13:16

2 Answers 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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hello, but I tried objectTree.ItemContainerStyle = resourceDictionary["TopologyTreeStyle"] as Style, with a style, that works.
My point was to give you idea how you can do it. Rest is all on you.
Sorry, I found what I have tried is with << objectTree.ItemContainerStyle = resourceDictionary["TopologyTreeStyle"] as Style >> , your code doesn't work...
Hello, OK finally your code is correct, but the point is to remove x:Key from XAML code. Thank you.
0

Finally I've found the answer in this topic :

Adding to a TreeView a ResourceDictionary from another Assembly

That works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.