C# 在WPF中重用XAML

C# 在WPF中重用XAML,c#,wpf,xaml,C#,Wpf,Xaml,我有一些XAML要重用。我可以很容易地创建一个自定义控件并使用它,但我不希望这样。以下是我尝试过的: <Window.Resources> <Expander x:Key="Namespacer" x:Shared="False" Name="NS" Background="SkyBlue"> <StackPanel Name="ClientArea" Margin="20,0,20,0"> <S

我有一些XAML要重用。我可以很容易地创建一个自定义控件并使用它,但我不希望这样。以下是我尝试过的:

    <Window.Resources>
    <Expander x:Key="Namespacer" x:Shared="False" Name="NS"  Background="SkyBlue">
        <StackPanel Name="ClientArea" Margin="20,0,20,0">
            <StackPanel Name="Usings" Grid.Row="0" Height="Auto"></StackPanel>
            <StackPanel Name="Structs" Grid.Row="1" Height="Auto"></StackPanel>
            <StackPanel Name="Classes" Grid.Row="2" Height="Auto"></StackPanel>
            <StackPanel Name="IFaces" Grid.Row="3" Height="Auto"></StackPanel>
            <StackPanel Name="Delegates" Grid.Row="4" Height="Auto"></StackPanel>
            <StackPanel Name="Enums" Grid.Row="5" Height="Auto"></StackPanel>
            <StackPanel Name="Nested" Grid.Row="6" Height="Auto"></StackPanel>
        </StackPanel>
    </Expander>
</Window.Resources>

<StackPanel>
    <ContentControl Name="N1" Content="{StaticResource Namespacer}" />

</StackPanel>

并且能够以类似的方式将新的XAML块添加到N1中的堆栈面板中。如何做到这一点?

好吧,你可以这样做:

((Expander)(this.N1.Content)).Header = "SomeTitle.Namespace1";
但这会变得丑陋。我建议切换到数据绑定。这里有一个例子

首先,这里是一个数据类,它的结构我想你会选择:

  public partial class MainWindow : Window
  {
    public class MyData
    {
      public string ItemTitle { get; set; }
      public IList<string> Usings { get; set; }
      public IList<string> Structs { get; set; }
    }

    public class MyViewModel
    {
      public IList<MyData> MyBoundData { get; set; }
    }

    public MainWindow()
    {
      var d1 = new MyData{
        ItemTitle = "thing1",
        Usings = new[]{"a", "b"}
      };
      var d2 = new MyData{
        ItemTitle = "thing2",
        Structs = new[]{"c","d"}
      };
      this.DataContext = new MyViewModel{
        MyBoundData = new[]{ d1, d2}
      };
      InitializeComponent();
    }
  }
公共部分类主窗口:窗口
{
公共类MyData
{
公共字符串ItemTitle{get;set;}
公共IList使用{get;set;}
公共IList结构{get;set;}
}
公共类MyViewModel
{
公共IList MyBoundData{get;set;}
}
公共主窗口()
{
var d1=新的MyData{
ItemTitle=“thing1”,
Usings=new[]{“a”,“b”}
};
var d2=新的MyData{
ItemTitle=“thing2”,
Structs=new[]{“c”,“d”}
};
this.DataContext=新的MyViewModel{
MyBoundData=new[]{d1,d2}
};
初始化组件();
}
}
下面是绑定到数据的items控件:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <ItemsControl ItemsSource="{Binding MyBoundData}" Focusable="False">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <Expander Header="{Binding ItemTitle}" Background="SkyBlue">
            <StackPanel>
              <Expander Header="Usings"  Background="SkyBlue">
                <ItemsControl ItemsSource="{Binding Usings}"/>
              </Expander>
              <Expander Header="Structs" Background="SkyBlue">
                <ItemsControl ItemsSource="{Binding Structs}"/>
              </Expander>
            </StackPanel>
          </Expander>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
  </Grid>
</Window>

注意,items控件有一个与“Namespacer”xaml块相对应的数据模板。当然,如果希望在多个ItemsControl中使用DataTemplate块,可以像示例中那样将其移动到窗口资源中

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <ItemsControl ItemsSource="{Binding MyBoundData}" Focusable="False">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <Expander Header="{Binding ItemTitle}" Background="SkyBlue">
            <StackPanel>
              <Expander Header="Usings"  Background="SkyBlue">
                <ItemsControl ItemsSource="{Binding Usings}"/>
              </Expander>
              <Expander Header="Structs" Background="SkyBlue">
                <ItemsControl ItemsSource="{Binding Structs}"/>
              </Expander>
            </StackPanel>
          </Expander>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
  </Grid>
</Window>