Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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
C# 使用xaml中的键创建实例_C#_Wpf_Xaml - Fatal编程技术网

C# 使用xaml中的键创建实例

C# 使用xaml中的键创建实例,c#,wpf,xaml,C#,Wpf,Xaml,给定的资源字典: <GroupBox x:Key="Group" x:Shared="False"> <ItemsControl ItemsSource="{Binding Items}"> ... </ItemsControl> </GroupBox> <ItemsControl x:Key="Test" x:Shared="False" I

给定的
资源字典

<GroupBox x:Key="Group"
          x:Shared="False">
    <ItemsControl ItemsSource="{Binding Items}">
    ...
    </ItemsControl>
</GroupBox>
<ItemsControl x:Key="Test"
              x:Shared="False"
              ItemsSource="{Binding Items}">
...
</ItemsControl>

...
...

两个
ItemsControl
内容相同。是否可以避免重复相同的xaml(
相当大)?是否可以从
组中创建
测试
实例

您可以使用
内容控件

<ItemsControl x:Key="Test"
              x:Shared="False"
              ItemsSource="{Binding Items}">
</ItemsControl>
<GroupBox x:Key="Group" x:Shared="False">
    <ContentControl Content="{StaticResource Test}" />
</GroupBox>

请注意,
ItemsControl
资源必须在
GroupBox
资源之前定义


正如@grek40所指出的,您还可以将
GroupBox
Content
属性直接设置到
ItemsSource
资源,前提是
GroupBox
不包含任何其他控件。

您可以使用
ContentControl

<ItemsControl x:Key="Test"
              x:Shared="False"
              ItemsSource="{Binding Items}">
</ItemsControl>
<GroupBox x:Key="Group" x:Shared="False">
    <ContentControl Content="{StaticResource Test}" />
</GroupBox>

请注意,
ItemsControl
资源必须在
GroupBox
资源之前定义


正如@grek40所指出的,您还可以将
GroupBox
Content
属性直接设置到
ItemsSource
资源,前提是
GroupBox
不包含任何其他控件。

实际上,您可以直接设置内容

<GroupBox x:Key="Group" x:Shared="False" Content="{StaticResource Test}">

实际上,您可以直接设置内容

<GroupBox x:Key="Group" x:Shared="False" Content="{StaticResource Test}">

让它成为一个用户控件。@Clemens,聪明,甚至没有这样想。那么我不需要
只要
。但是我必须从外部传递所有内容(例如,在本例中,
itemsource
binding)。。。不确定长xaml是否更糟糕。还有其他方法吗?您可以从ItemsControl派生自定义控件,并在Generic.xaml中以默认样式设置其属性。看。把它变成一个用户控件。@Clemens,聪明的,甚至没有这样想。那么我不需要
只要
。但是我必须从外部传递所有内容(例如,在本例中,
itemsource
binding)。。。不确定长xaml是否更糟糕。还有其他方法吗?您可以从ItemsControl派生自定义控件,并在Generic.xaml中以默认样式设置其属性。参见.I of
x:Shared
。不确定
DataTemplate
是否更好,是否有相关链接?找不到“x:Shared vs DataTemplate”或类似内容。@Sinatr x:Shared的一个问题是,嵌套的
ResourceDictionary
中不允许使用它,但我手头没有一个好的比较源,如果x:Shared适合您,我不需要干预。只是想用DataTemplate方法在可用选项中添加一个替代设计选项。I of
x:Shared
。不确定
DataTemplate
是否更好,是否有相关链接?找不到“x:Shared vs DataTemplate”或类似内容。@Sinatr x:Shared的一个问题是,嵌套的
ResourceDictionary
中不允许使用它,但我手头没有一个好的比较源,如果x:Shared适合您,我不需要干预。只是想用DataTemplate方法在可用选项中添加一个替代设计选项。