すでに2つのユーザーコントロールを作成しました。1つをマスターユーザーコントロールとして使用し、もう1つを詳細ユーザーコントロールとして次のように使用します。
1 Parent Control
1.1 - User Control 1 as Master Control
1.2 - User Control 2 as Details Control
マスターコントロールには、アイテムの名前を選択するリストボックスがあり、詳細コントロールには、在庫のあるすべての利用可能なアイテムが表示されます。親コントロールにItemIdを追加し、それをマスターコントロールと詳細コントロールにバインドしました(両方のコントロールのDPとしてItemIdがあります)。マスターからアイテムを選択すると、詳細グリッドが更新されません。
マスターユーザーコントロールからアイテムを選択するときに、どうすれば確認できますか。詳細ユーザーコントロールに詳細が表示されますか?
親コントロール
<Grid>
<StackPanel Orientation="Horizontal"
Width="650"
HorizontalAlignment="Left"
Margin="10,10,0,0">
<UC:ItemDetailUC ItemId="{Binding ElementName=MainWindowName,Path=ItemId,Mode=TwoWay}" />
<UC:StockItemDetailsUC ItemId="{Binding ElementName=MainWindowName,Path=ItemId,Mode=TwoWay}"/>
</StackPanel>
</Grid>
。
public partial class MainWindow : Window, INotifyPropertyChanged
{
private int _ItemId;
public int ItemId
{
get
{
return _ItemId;
}
set
{
if (_ItemId == value)
return;
_ItemId = value;
OnPropertyChanged("ItemId");
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
1.1-マスターコントロールとしてのユーザーコントロール1-ItemDetailUC.XAML
<UserControl x:Class="LearnWPF.ItemDetailUC"
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"
mc:Ignorable="d"
Name="ItemDetailUCName"
d:DesignHeight="100" d:DesignWidth="350">
<Grid Background="Aqua">
<StackPanel Orientation="Vertical" Margin="10,10,0,0" >
<Label Content="Master Control:" Width="350" HorizontalContentAlignment="Center"/>
<StackPanel Orientation="Horizontal" Width="200" HorizontalAlignment="Left" Margin="10,10,0,0">
<Label Content="Item :" Width="80"/>
<ComboBox Name="ItemListComboBox" Width="100"
DisplayMemberPath="ItemName"
SelectedValuePath="ItemId"
SelectedValue="{Binding ElementName=ItemDetailUCName, Path=ItemId}" />
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10,10,0,0">
<Label Content="Available Qty :" Width="80"/>
<TextBox Width="100" Text="{Binding ElementName=ItemListComboBox, Path=SelectedItem.AvailableQty}" />
<Label Content="MaxQty :" Width="60"/>
<TextBox Width="80" Text="{Binding ElementName=ItemListComboBox, Path=SelectedItem.MaxQty}" />
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
。
public static readonly DependencyProperty ItemIdProperty = DependencyProperty.Register("ItemId", typeof(int), typeof(ItemDetailUC));
public int ItemId
{
get
{
return (int)GetValue(ItemIdProperty);
}
set
{
SetValue(ItemIdProperty, value);
}
}
public ItemDetailUC()
{
InitializeComponent();
ItemListComboBox.ItemsSource = Data.GetItemList();
this.DataContext = this;
}
1.2-詳細コントロールとしてのユーザーコントロール2
<UserControl x:Class="LearnWPF.StockItemDetailsUC"
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"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="300">
<Grid>
<StackPanel Orientation="Vertical" Background="Aquamarine">
<Label Content="Details Control:" Width="300" HorizontalContentAlignment="Center"/>
<DataGrid Name="StockItemDetailsDataGrid" Background="Aquamarine"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Location Name" Binding="{Binding LocationName}"/>
<DataGridTextColumn Header="RowNo" Binding="{Binding RowNo}" />
<DataGridTextColumn Header="ColumnNo" Binding="{Binding ColumnNo}" />
<DataGridTextColumn Header="Qty" Binding="{Binding Qty}" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
</UserControl>
。
public static readonly DependencyProperty ItemIdProperty = DependencyProperty.Register("ItemId", typeof(int), typeof(StockItemDetailsUC));
public int ItemId
{
get
{
return (int)GetValue(ItemIdProperty);
}
set
{
SetValue(ItemIdProperty, value);
}
}
public StockItemDetailsUC()
{
InitializeComponent();
Loaded += new RoutedEventHandler(StockItemDetailsUC_Loaded);
}
void StockItemDetailsUC_Loaded(object sender, RoutedEventArgs e)
{
if (ItemId != 0)
{
StockItemDetailsDataGrid.ItemsSource = Data.GetItemLocaitonDetails(ItemId);
}
this.DataContext = this;
}
.
public class ItemDetailsVO: INotifyPropertyChanged
{
private int _ItemId;
public int ItemId
{
get
{
return _ItemId;
}
set
{
if (_ItemId == value)
return;
_ItemId = value;
OnPropertyChanged("ItemId");
}
}
private String _ItemName;
private int _AvailableQty;
private int _MaxQty;
}
public class StockItemDetails : INotifyPropertyChanged
{
private int _ItemId;
public int ItemId
{
get
{
return _ItemId;
}
set
{
if (_ItemId == value)
return;
_ItemId = value;
OnPropertyChanged("ItemId");
}
}
private String _LocationName;
private int _Qty;
private int _RowNo;
private int _ColumnNo;
/..... all properties are implemented
}