Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use two user controls as Master –Details
    primarykey
    data
    text
    <p>I have two User Controls already created now I want to use one as Master User Control and another as Details User control as follows:</p> <pre><code>1 Parent Control 1.1 - User Control 1 as Master Control 1.2 - User Control 2 as Details Control </code></pre> <p>The Master Control has the List box where I select the name of the Item and the Details Control Displays the all the available Items in stock. I have Added ItemId in the Parent Control and bound it to the Master Control and Details Control (both controls have ItemId as DP). When I select the Item from Master the details Grid is not refreshing.</p> <p>How can I make sure that when I select the Item from Master User Control; the Detail User Control should show me the details?</p> <p>The Parent Control</p> <pre><code> &lt;Grid&gt; &lt;StackPanel Orientation="Horizontal" Width="650" HorizontalAlignment="Left" Margin="10,10,0,0"&gt; &lt;UC:ItemDetailUC ItemId="{Binding ElementName=MainWindowName,Path=ItemId,Mode=TwoWay}" /&gt; &lt;UC:StockItemDetailsUC ItemId="{Binding ElementName=MainWindowName,Path=ItemId,Mode=TwoWay}"/&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; </code></pre> <p>.</p> <pre><code>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)); } } } </code></pre> <p>1.1 - User Control 1 as Master Control - ItemDetailUC.XAML</p> <pre><code> &lt;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"&gt; &lt;Grid Background="Aqua"&gt; &lt;StackPanel Orientation="Vertical" Margin="10,10,0,0" &gt; &lt;Label Content="Master Control:" Width="350" HorizontalContentAlignment="Center"/&gt; &lt;StackPanel Orientation="Horizontal" Width="200" HorizontalAlignment="Left" Margin="10,10,0,0"&gt; &lt;Label Content="Item :" Width="80"/&gt; &lt;ComboBox Name="ItemListComboBox" Width="100" DisplayMemberPath="ItemName" SelectedValuePath="ItemId" SelectedValue="{Binding ElementName=ItemDetailUCName, Path=ItemId}" /&gt; &lt;/StackPanel&gt; &lt;StackPanel Orientation="Horizontal" Margin="10,10,0,0"&gt; &lt;Label Content="Available Qty :" Width="80"/&gt; &lt;TextBox Width="100" Text="{Binding ElementName=ItemListComboBox, Path=SelectedItem.AvailableQty}" /&gt; &lt;Label Content="MaxQty :" Width="60"/&gt; &lt;TextBox Width="80" Text="{Binding ElementName=ItemListComboBox, Path=SelectedItem.MaxQty}" /&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>.</p> <pre><code> 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; } </code></pre> <p>1.2 - User Control 2 as Details Control</p> <pre><code>&lt;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"&gt; &lt;Grid&gt; &lt;StackPanel Orientation="Vertical" Background="Aquamarine"&gt; &lt;Label Content="Details Control:" Width="300" HorizontalContentAlignment="Center"/&gt; &lt;DataGrid Name="StockItemDetailsDataGrid" Background="Aquamarine" AutoGenerateColumns="False"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Location Name" Binding="{Binding LocationName}"/&gt; &lt;DataGridTextColumn Header="RowNo" Binding="{Binding RowNo}" /&gt; &lt;DataGridTextColumn Header="ColumnNo" Binding="{Binding ColumnNo}" /&gt; &lt;DataGridTextColumn Header="Qty" Binding="{Binding Qty}" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>.</p> <pre><code> 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 } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload