做了一个小例子向实现通过ContentControl来切换不同的子控件并绑定子控件数据的功能,但是第二个控件无法绑定数据,请问怎么解决?下面是全部代码:
负责切换的窗体前台代码
<Window x:Class="WpfEasyDemo.ListBoxDemo01"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfEasyDemo"
mc:Ignorable="d"
Title="ListBoxDemo" Height="300" Width="400" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox x:Name="lstInstrumentsShownPattern" ItemsSource="{Binding ItemsPattern}"
Grid.Column="0" Margin="10,10" SelectedIndex="0">
<ListBox.ToolTip>
<StackPanel>
<TextBlock Text="pattern selection" />
</StackPanel>
</ListBox.ToolTip>
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Border x:Name="itemContentBorder" BorderBrush="Blue" BorderThickness="1"
Grid.Column="1" Margin="10,10">
<ContentControl
Content="{Binding ElementName=lstInstrumentsShownPattern, Path=SelectedItem.Pattern}" />
</Border>
</Grid>
</Window>
切换窗体的后台代码
using System.Windows;
using GalaSoft.MvvmLight;
namespace WpfEasyDemo
{
/// <summary>
/// ListBoxDemo02.xaml 的交互逻辑
/// </summary>
public partial class ListBoxDemo01 : Window
{
public ListBoxDemo01()
{
InitializeComponent();
_itemsPattern= new[]
{
new ListPattern("Grid","It's grid", new ListPatternGridUC()),
new ListPattern("Card","It's card", new ListPatternCardUC()),
};
DataContext = this;
}
#region Remark
private string _remark="This is Remark";
public string Remark
{
get { return _remark; }
set { _remark = value; }
}
#endregion
#region ItemsPattern
private ListPattern[] _itemsPattern;
public ListPattern[] ItemsPattern
{
get { return _itemsPattern; }
private set { _itemsPattern = value; }
}
#endregion
}
public class ListPattern: ViewModelBase
{
#region Name
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
#endregion
#region Description
private string _description;
public string Description
{
get { return _description; }
set { _description = value; }
}
#endregion
#region Pattern
private object _pattern;
public object Pattern
{
get { return _pattern; }
set { Set(ref _pattern, value); }
}
#endregion
#region Constructor
public ListPattern(string name,string description, object pattern)
{
_name = name;
_description = description;
Pattern = pattern;
}
#endregion
}
}
================
用户控件1的Xaml
<UserControl x:Class="WpfEasyDemo.ListPatternGridUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfEasyDemo02"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.Remark}"/>
<DataGrid Grid.Row="1" AutoGenerateColumns="False" CanUserDeleteRows="False" CanUserAddRows="False"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.ItemsPattern}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
================
用户控件2的Xaml代码
<UserControl x:Class="WpfEasyDemo.ListPatternCardUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfEasyDemo02"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Card"/>
<ListBox Grid.Row="1"
BorderThickness="1"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.ItemsPattern}"
HorizontalContentAlignment="Left"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<!--器械标题-->
<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Name}"/>
<!--器械数量-->
<Label Grid.Row="1" Grid.Column="1" Content="{Binding Description}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</UserControl>
解决了吗?