2017-09-04 2 views
1

내 기본 창에서 여러 컨트롤을 사용하고 있습니다. 상단에 한 그리드는 라이센스의 목록이 그와 나는 라이센스 정보를부모 변경시 바인드 된 자식 wpf 업데이트

은 MainWindow를 내가 그리드의 선택을 변경할 때마다 표시 할 :

<dx:DXWindow 
x:Class="LicenceManagerWPF.Forms.frmCustomerLicense" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
dx:ThemeManager.ThemeName="Office2016" 
xmlns:ctr="clr-namespace:LicenceManagerWPF.Controls" 
Title="CustomerLicence" Height="800" Width="1000" 
WindowStartupLocation="CenterScreen" Loaded="DXWindow_Loaded"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="50"/> 
     <RowDefinition MinHeight="200" Height="200*"/> 
     <RowDefinition Height="200*"/> 
     <RowDefinition MinHeight="25" Height="25"/> 
    </Grid.RowDefinitions> 
    <StackPanel x:Name="Stack_Top" Orientation="Horizontal" Grid.Row="0" > 
     <dx:SimpleButton x:Name="btnRefresh" Style="{StaticResource ResourceKey=BtnSmall}" ToolTip="Refresh Licenses" Glyph="{dx:DXImage Image=Refresh_32x32.png}" Content="Resfresh" /> 
     <dx:SimpleButton x:Name="btndNew" Style="{StaticResource ResourceKey=BtnSmall}" ToolTip="New License" Glyph="{dx:DXImage Image=New_32x32.png}" Content="New Customer" /> 
     <dx:SimpleButton x:Name="btnDelete" Style="{StaticResource ResourceKey=BtnSmall}" ToolTip="Delete Licence" Content="Delete" Glyph="{dx:DXImage Image=Cancel_32x32.png}"/> 
     <dx:SimpleButton x:Name="btnEdit" Style="{StaticResource ResourceKey=BtnSmall}" ToolTip="Edit Customer" Glyph="{dx:DXImage Image=EditContact_32x32.png}" /> 
     <TextBlock Text="Customer: " FontSize="20" Margin="5"/> 
     <TextBlock Text="{Binding Customer.Name}" Margin="5" FontSize="20"/> 
    </StackPanel> 
    <ctr:Licences x:Name="grdLicenses" Grid.Row="1"> 
    </ctr:Licences> 
    <Grid Grid.Row="2"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="auto"/> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions>    
     <ctr:LicenseDetail x:Name="ct_LicenseDetail" Grid.Column="0"/> 
     <ctr:LicenceLog x:Name="ct_LicenseLog" Grid.Column="1"/> 
    </Grid> 
</Grid> 

이 그리드는을 ctr_Licenses이고 다른 컨트롤은 LicenseDetail입니다.이 컨트롤은 선택된 라이센스의 정보를 보여 주며, licenseDetail 컨트롤입니다.

<UserControl 
     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:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" 
     xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
     xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" x:Class="LicenceManagerWPF.Controls.LicenseDetail" 
     mc:Ignorable="d" 
     d:DesignHeight="500" d:DesignWidth="600" x:Name="ctrl_LicenseDetail"   > 
<UserControl.Resources> 
    <Style x:Key="SmallMargin" TargetType="FrameworkElement"> 
     <Setter Property="Margin" Value="5"/> 
    </Style> 
    <Style x:Key="TabMargin" TargetType="FrameworkElement" > 
     <Setter Property="Margin" Value="2.5"/> 
    </Style> 
    <Style x:Key="SmalllMarginTextBlock" BasedOn="{StaticResource SmallMargin}" TargetType="{x:Type TextBlock}">    
    </Style> 
    <Style x:Key="SmallMarginDropDown" BasedOn="{StaticResource SmallMargin }" TargetType="dx:DropDownButton" >    
    </Style> 
    <Style x:Key="SmallMarginText" BasedOn="{StaticResource SmallMargin}" TargetType="TextBox">    
    </Style> 
    <Style x:Key="ckkStyle" TargetType="dxe:CheckEdit" BasedOn="{StaticResource TabMargin}"> 
     <Setter Property="IsReadOnly" Value="True"/> 
    </Style>    
</UserControl.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="40" /> 
     <RowDefinition Height="30" /> 
     <RowDefinition Height="30" /> 
     <RowDefinition Height="30"/> 
     <RowDefinition Height="100*"/>    
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="100" /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <TextBlock Text="License Detail" FontSize="16" Grid.ColumnSpan="2" VerticalAlignment="Center" Margin="10" Grid.Row="0" /> 
    <TextBlock Text="Serial Number:" Grid.Column="0" Grid.Row="1" Style="{StaticResource ResourceKey=SmalllMarginTextBlock}"/> 
    <TextBlock Text="{Binding Path=DataContext.SelectedLicense.SerialNumber, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=Window}}" x:Name="txtSerialNum" Grid.Column="1" Grid.Row="1" Style="{StaticResource ResourceKey=SmalllMarginTextBlock}"/> 
    <TextBlock Text="Product:" Grid.Column="0" Grid.Row="2" Style="{StaticResource ResourceKey=SmalllMarginTextBlock}" /> 
    <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="1"> 
     <dx:DropDownButton x:Name="dropDownButton" Width="180" Content="Product" Style="{StaticResource SmallMarginDropDown}">      
     </dx:DropDownButton> 
     <TextBlock Text="Status:" Style="{StaticResource SmalllMarginTextBlock}" /> 
     <TextBlock Text="" Width="150" Style="{StaticResource SmalllMarginTextBlock}" /> 
    </StackPanel> 
    <TextBlock Text="Description:" Style="{StaticResource SmalllMarginTextBlock}" Grid.Row="3" Grid.Column="0" /> 
    <TextBox Text="" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="3" Style="{StaticResource SmallMargin}" Width="350" /> 
    <dx:DXTabControl Grid.Row="4" Grid.ColumnSpan="2"> 
     <dx:DXTabItem x:Name="tabAttributes" Header="License Attributes" > 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="30" /> 
        <RowDefinition Height="30" /> 
        <RowDefinition Height="30" /> 
        <RowDefinition Height="30"/> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="120" /> 
        <ColumnDefinition /> 
       </Grid.ColumnDefinitions> 
       <TextBlock Text="Activation Mode:" Style="{StaticResource TabMargin}" /> 
       <dx:DropDownButton Grid.Row="0" Grid.Column="1" Width="100" HorizontalAlignment="Left" Style="{StaticResource TabMargin}" ButtonKind="Repeat" /> 
       <TextBlock Text="Grace Period:" Style="{StaticResource TabMargin}" Grid.Row="1"/> 
       <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal"> 
        <dxe:SpinEdit Width="80" Style="{StaticResource TabMargin}" MinValue="0" MaxValue="100" IsReadOnly="True" Value="10" Mask="n0"/> 
        <TextBlock Text="Days" Style="{StaticResource TabMargin}" /> 
       </StackPanel> 
       <dxe:CheckEdit Grid.Row="2" Content="Expiration Date" IsChecked="False" /> 
       <dxe:DateNavigator Grid.Row="2" Grid.Column="1" Width="185" HorizontalAlignment="Left" Style="{StaticResource TabMargin}" /> 
       <TextBlock Text="Max Computers:" Grid.Row="3" Style="{StaticResource TabMargin}"/> 
       <dxe:SpinEdit Grid.Row="4" Grid.Column="1" HorizontalAlignment="Left" Width="80" Margin="2.5,2.5,2.5,1" MinValue="0" MaxValue="100" IsReadOnly="True" Value="10" Mask="n0"/> 
      </Grid> 
     </dx:DXTabItem> 
     <dx:DXTabItem Header="Product Features" x:Name="tabProductFeatures" Visibility="Visible"> 
      <StackPanel Orientation="Vertical" > 
       <dxe:CheckEdit Content="Standard" Name="chkStandard" IsReadOnly="True" Style="{StaticResource ckkStyle}" /> 
       <dxe:CheckEdit Content="Admin Mode" Name="chkAdminMode" IsReadOnly="True" Style="{StaticResource ckkStyle}" /> 
       <dxe:CheckEdit Content="Batch Mode" Name="chkBatchMode" IsReadOnly="True" Style="{StaticResource ckkStyle}" /> 
       <dxe:CheckEdit Content="Custom Data Series" Name="chkCustomDataSeries" IsReadOnly="True" Style="{StaticResource ckkStyle}" /> 
       <dxe:CheckEdit Content="Local DataBase" Name="LocalDataBase" IsReadOnly="True" IsChecked="True" Style="{StaticResource ckkStyle}"/> 
       <dxe:CheckEdit Content="Cloud Data Base" Name="CloudDataBase" Style="{StaticResource ckkStyle}" /> 
      </StackPanel>    
     </dx:DXTabItem> 
     <dx:DXTabItem Name="tabComputers" Header="Computers"> 
      <dxg:GridControl Name="grdComputers">      
      </dxg:GridControl> 
     </dx:DXTabItem> 
     <dx:DXTabItem Name="tabMain_Schedule" Header="Maint. Schedule"> 
      <dxg:GridControl>      
      </dxg:GridControl> 
     </dx:DXTabItem> 
     <dx:DXTabItem x:Name="tabFeaturePB" Header="Product Features"> 
      <StackPanel Orientation="Vertical"> 
       <dxe:CheckEdit Content="pb" Name="chkpb" IsReadOnly="True" Style="{StaticResource ckkStyle}" /> 
       <dxe:CheckEdit Content="pb2" Name="chkpb2" IsReadOnly="True" Style="{StaticResource ckkStyle}" />      
      </StackPanel> 
     </dx:DXTabItem> 

    </dx:DXTabControl>   
</Grid> 

mainwindows의의 DataContext 클래스는 직렬로 된 본체를 채우는이 부분 메신저에서이

public class CustomerLicenses 
{ 
    private Customer _Customer; 
    private LicenseList _Licenses; 
    private License _SelectedLicense; 
    //private Guid _SelectedSerial; 
    //public delegate void SelectLicenseEventArg(String SerialNum); 
    //public event SelectLicenseEventArg SelectLicense; 


    public CustomerLicenses(Customer Customer) 
    { 
     _Customer = Customer; 
    } 

    public CustomerLicenses(Customer customer, LicenseList licenses) 
    { 
     _Customer = customer; 
     _Licenses = licenses; 
    } 

    public CustomerLicenses() 
    { 
     _Customer = new Customer(); 
     _Licenses = new LicenseList(); 
    } 

    [DataMember] 
    public Customer Customer 
    { 
     get { return _Customer; } 
     set 
     { 
      if (_Customer != value) 
      { 
       this._Customer = value; 
       this.NotifyPropertyChanged("Customer"); 
      } 
     } 
    } 

    [DataMember] 
    public LicenseList Licenses 
    { 
     get { return _Licenses;} 
     set 
     { 
      if (_Licenses != value) 
      { 
       this._Licenses = value; 
       this.NotifyPropertyChanged("Licenses"); 
      } 
     } 
    } 


    private DataTable GetLicensesTable() 
    { 
     var dt = new DataTable(); 
     dt.Columns.AddRange(new DataColumn []{ 
      new DataColumn("SerialNumber",typeof(string)), 
      new DataColumn("Product",typeof(string)), 
      new DataColumn("Status",typeof(string)), 
      new DataColumn("ActivationMode",typeof(string)), 
      new DataColumn("MaxComputers", typeof(int)), 
      new DataColumn("NumActive",typeof(int)), 
      new DataColumn("Description",typeof(string)), 
      new DataColumn("License",typeof(object)) 
     }); 
     _Licenses.ForEach((x) => { 
      var rw = dt.NewRow(); 
      rw["SerialNumber"] = x.SerialNumber; 
      rw["Product"] = x.Product.Name; 
      rw["Status"] = x.Status; 
      rw["ActivationMode"] = Enum.GetName(typeof(ActivationModeEnum), x.ActivationMode); //x.ActivationMode; 
      rw["MaxComputers"] = x.MaxComputers; 
      rw["NumActive"] = Activated(x.Product.ProductId); 
      rw["Description"] = x.Description; 
      rw["License"] = x; 
      dt.Rows.Add(rw); 
     }); 
     return dt; 
    }   

    public DataTable LicensesTable{ 
     get { return GetLicensesTable(); } 
    } 

    [DataMember] 
    public License SelectedLicense { 
     get { return _SelectedLicense;} 
     set { 
      this.NotifyPropertyChanged("SelectedLicense"); 
      _SelectedLicense = value; 
     } 
    }  
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; 
    public void NotifyPropertyChanged(string propName) 
    { 
     if (this.PropertyChanged != null) 
      this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propName)); 

    } 
} 

이며, 내가 응용 프로그램을 시작할 때 표시 괜찮아`원인은 경우 처음 라이센스를 필요하지만 난 텍스트가 변경되지 않는 다른 라이센스를 선택하십시오.

TextBlock Text="{Binding Path=DataContext.SelectedLicense.SerialNumber, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=Window}}" x:Name="txtSerialNum" Grid.Column="1" Grid.Row="1" Style="{StaticResource ResourceKey=SmalllMarginTextBlock}" 

내가 값을 assaing 어떻게하면 그리드 chanege 선택 항목이 표에서 선택했을 때 내가 아이를 업데이트 할 수있는 방법

private void GridRowSelected (object sender, SelectedItemChangedEventArgs e) 
    { 
     try 
     { 
      var Record = (DataRowView)grdLicenses.grdLicences.SelectedItem; 
      var SelectedLicense = (License)Record["License"]; 
      _CustomerLicense.SelectedLicense = SelectedLicense;     

      //Customer_Detail.DataContext = SelectedCustomer; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

. 당신이 말한대로

감사

+0

로 변경 한 propertyfotify을 구현 잘못된 경우의 frome 크로스 세터 고객 속성 if (_Customer! = value) { this._Customer = value; this.NotifyPropertyChanged ("Customer"); }. 그리고 rowSelected 그냥 'CustomerLicense.Customer = CUstomerLicensce.Customer' – sTrenat

+0

빠른 두 번째 방법은 뭔가 쓸 것입니다 개인 무효 Notify 구현하는 것입니다; this.NotifyPropertyChanged ("Customer"); 그리고 gridRow selected에서이 메소드를 호출하십시오. 마지막 방법은 실제로 관찰 할 수있는 콜렉션을 구현하는 것이고, 스택에 대한 대답은 어떻게 할 것인가입니다. – sTrenat

답변

0

sTrenat, 내가 클래스를 확인하고 난 가장 빠른 방법은 제거하는 것입니다

내가이

[DataContract] 
public class CustomerLicenses: System.ComponentModel.INotifyPropertyChanged 
{ 
    public void NotifyPropertyChanged(string propName) 
    { 
     System.ComponentModel.PropertyChangedEventHandler handler = PropertyChanged; 
     if (this.PropertyChanged != null) 
      this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propName)); 
    } 
} 
관련 문제