父改变wpf时更新绑定子wpf

时间:2017-09-04 03:03:56

标签: wpf events data-binding

我在主窗口中使用了几个控件。 顶部的一个网格,其中包含许可证列表,我想在每次更改网格选择时显示许可证的信息

主窗口:

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

主窗口的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}"

这就是我在选择网格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);
        }
    }

如何在网格中选择项目时更新孩子。

此致

1 个答案:

答案 0 :(得分:0)

sTrenat,正如你所说我检查了课程,我错误地实施了propertyfotify

我改变了这个

[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));
    }
}