2012-06-23 13 views
0

我有兩個用戶控件已創建的,現在我想用一個爲主機的用戶控制和其他的詳細用戶控制如下:如何使用兩個用戶控件作爲主-details

1 Parent Control 
    1.1 - User Control 1 as Master Control 
    1.2 - User Control 2 as Details Control 

主控制有列表框中我選擇項目的名稱和細節控件顯示庫存中的所有可用項目。 我在父控件中添加了ItemId並將其綁定到主控件和詳細控件(兩個控件都將ItemId作爲DP)。 當我從主文件中選擇項目時,細節網格不會令人耳目一新。

如何確保當我從主用戶控件中選擇項目時;細節用戶控件應該向我顯示細節?

家長控制

<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 
    } 

回答

0

不要只使用ItemID作爲依賴項屬性,而是使用項目本身。以下方案應滿足您的要求:

假設CItem是主/明細視圖中一條記錄的類型。在託管主控和詳細控件的視圖中,使用公開的List<CItem>的視圖模型,或者如果您動態地要添加/刪除項目ObservableCollection<CItem>,您將其設置爲主列表的ItemsSource的綁定目標。另外,在視圖模型中創建類型爲CItem的依賴項屬性「SelectedObject」。在SelectedItem的主列表中創建一個雙向綁定到該「SelectedObject」屬性。在詳細查看剛纔還結合「SelectedObject」的屬性,如

<TextBox Text="{Binding SelectedObject.MyProperty, Mode=TwoWay}" /> 

現在,當在主視圖中的用戶選擇不同的記錄SelectedObject屬性,都會更新,歸因於對所有細節的綁定也將獲得更新。

編輯: 因此,如果您需要額外的邏輯,請考慮在依賴項屬性ItemID的值發生更改時使用回調函數。註冊依賴項屬性時使用PropertyMetadataPropertyChangedCallback constructor。每當依賴項屬性的值發生更改時,都會調用回調函數。在這個回調函數中,您可以觸發您提到的用於加載/準備細節視圖的項目的附加邏輯。

+0

在我的情況下,Details Control有一些額外的邏輯來獲取不是主記錄部分的數據。我添加了更好的理解問題的代碼。 – user1476446

+0

非常感謝您的精彩解決方案。有效。 – user1476446

相關問題