Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 公开UserControl中的子控件以在XAML中使用_Wpf_Xaml - Fatal编程技术网

Wpf 公开UserControl中的子控件以在XAML中使用

Wpf 公开UserControl中的子控件以在XAML中使用,wpf,xaml,Wpf,Xaml,我有一个包含TreeView的UserControl。我希望用户能够通过XAML设置内部TreeView控件的属性,但我不知道如何做到这一点 我尝试在树视图的UserControl上创建公共属性,但这只允许我设置SelectedItemChanged触发器 我想做一些类似的事情: <ExampleUserControl> <ExampleUserControl.TreeView.ItemTemplate> ... </ExampleU

我有一个包含TreeView的UserControl。我希望用户能够通过XAML设置内部TreeView控件的属性,但我不知道如何做到这一点

我尝试在树视图的UserControl上创建公共属性,但这只允许我设置SelectedItemChanged触发器

我想做一些类似的事情:

<ExampleUserControl>
    <ExampleUserControl.TreeView.ItemTemplate>
        ...
    </ExampleUserControl.TreeView.ItemTemplate>
</ExampleUserControl>
或:


我不希望在UserControl中为每个TreeView属性创建属性,也不希望强制用户在C中定义控件。

至于将多个属性传递给用户控件中的子控件,您可以始终公开样式属性

童装

对于ItemsSource,除非您使用[Josh Smith的Element Spy/Data Context Spy/Freezable][1]技巧,否则您将在DataContext上断开连接

因此,要么你使用这些技巧,要么就有两个属性

1项目来源 2儿童风格

xaml最终

    <ChildTreeAnswer:MyControl ItemsSource="{Binding Items}">
        <ChildTreeAnswer:MyControl.ChildStyle>
            <Style>
                <Setter Property="ItemsControl.ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Border BorderBrush="Black"
                                    BorderThickness="1"
                                    Margin="5">
                                <TextBlock Text="{Binding }" />
                            </Border>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ChildTreeAnswer:MyControl.ChildStyle>                           
    </ChildTreeAnswer:MyControl>
然后在用户控件中执行。。。为了简单起见,我使用了一个列表框

    <ListBox ItemsSource="{Binding ItemsSource}"
             Style="{Binding ChildStyle}" />

不能在xaml标记中堆叠属性。而且你可能无法将树视图公开为公共财产;无论如何,这将是一个糟糕的绝句。很可能需要在usercontrol上创建dp并使用模板绑定。
    <ListBox ItemsSource="{Binding ItemsSource}"
             Style="{Binding ChildStyle}" />