سؤال

I'm completely new to XAML and WPF, so maybe (hopefully) there's an easy solution to this.

This is basically what the tree should look like:

☑ MyViewModel
|_Property1
|
_Property2

☑ MyViewModel
|_Property1
|
_Property2

And here's the code:

<HierarchicalDataTemplate x:Uid="HierarchialDataTemplate2" DataType="{x:Type vm:MyViewModel}" ItemTemplate="{StaticResource Template1}">

     <HierarchialDataTemplate.ItemContainerStyle>
          <!-- style information -->
     </HierarchialDataTemplate.ItemContainerStyle>

     <HierarchialDataTemplate.ItemSource>
          <MultiBinding x:Uid="MultiBinding1" Converter="{StaticResource CollectionConcatinator}">
               <Binding x:Uid="Binding1" Path="Property1"/>
               <Binding x:Uid="Binding2" Path="Property2"/>
          </MultiBinding>
     </HierarchialDataTemplate.ItemSource>

     <StackPanel x:Uid="frame" x:Name="frame" Orientation="Horizontal">
          <!-- basic checkbox and name for top level of the tree node-->
     </StackPanel>

</HierarchialDataTemplate>

I want to apply Template1 to only Property1, while not applying any template to Property2. Is there any way to do that?

I tried using a ContentPresenter for Property1, but I could only get it to show up in the top level of the tree, along side the checkbox and name. This is the ContentPresenter I used:

<ContentPresenter x:Uid="Property1Content" x:Name="Property1Content" Content="{Binding Property1} ContentTemplate={StaticResource Template1}"> </ContentPresenter>

If I try to put the ContentPresenter anywhere besides in the StackPanel I got an error.

هل كانت مفيدة؟

المحلول

You can use Treeview's ItemTemplateSelector

public class TreeViewTemplateSelector : DataTemplateSelector
{

    private DataTemplate _Template1;
    public DataTemplate Template1
    {
        get { return _Template1; }
        set { _Template1 = value; }
    }

    private DataTemplate _Template2;
    public DataTemplate Template2
    {
        get { return _Template2; }
        set { _Template2 = value; }
    }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        return item.GetType() == typeof(property1) ? Template1 : Template2;
    }

}

Put below xaml inside Treeview

<TreeView.ItemTemplateSelector>
    <yourNamespace:TreeViewTemplateSelector Template1="{StaticResource template1}" Template2="{StaticResource template1}">
    </c:TreeViewTemplateSelector>
</TreeView.ItemTemplateSelector>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top