WPF:如何将Datagrid绑定到导航属性

时间:2017-01-02 11:27:42

标签: c# wpf xaml datagrid

我是WPF和.NET的新手。 我正在开发一个REST服务的客户端。 我有两个班:

namespace TcNFCCommons
{
    using System;
    using System.Collections.Generic;

    public partial class Tag
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Tag()
        {
            this.PartecipanteTag = new HashSet<PartecipanteTag>();
            this.Device = new HashSet<Device>();
        }

        public int Id { get; set; }
        public string SerialeEvento { get; set; }
        public int IdTipo { get; set; }
        public string Label { get; set; }
        public int Cariche { get; set; }
        public bool Nascosto { get; set; }
        public bool Ricarica { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<PartecipanteTag> PartecipanteTag { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Device> Device { get; set; }
        public virtual Evento Evento { get; set; }
        public virtual TipoTag TipoTag { get; set; }
    }
}

namespace TcNFCCommons
{
    using System;
    using System.Collections.Generic;

    public partial class Device
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Device()
        {
            this.Tag = new HashSet<Tag>();
        }

        public string Id { get; set; }
        public string Label { get; set; }
        public string Tipo { get; set; }
        public string Note { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Tag> Tag { get; set; }
    }
}

我想创建两个Datagrids:

  1. 在我要管理设备实体的第一部分
  2. 当我选择一个设备时,我想显示与所选设备关联的标签列表
  3. 如果我删除第二个数据网格中的一行,我想要从所选设备中取消关联该标记
  4. 如果我添加一行,我想从显示所有现有标记的组合中选择要与所选设备关联的标记
  5. 我能够完成第一步,但我不知道如何正确绑定第二个数据网格,使其按照其他3个步骤中的描述工作。

    这是我的XAML:

    <UserControl x:Class="TcNFCBackEnd.devices.DevicesControl"
                 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:local="clr-namespace:TcNFCBackEnd.devices"
                 xmlns:tags="clr-namespace:TcNFCBackEnd.tags"
                 xmlns:system="clr-namespace:System;assembly=mscorlib"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="1000">
        <UserControl.Resources>
            <ObjectDataProvider x:Key="DeviceData" ObjectType="{x:Type local:DevicesProvider}" MethodName="GetObjects">
                <ObjectDataProvider.MethodParameters>
                    <x:Null />
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
            <ObjectDataProvider x:Key="TagData" ObjectType="{x:Type tags:TagsProvider}" MethodName="GetObjects">
                <ObjectDataProvider.MethodParameters>
                    <x:Null />
                </ObjectDataProvider.MethodParameters>
            </ObjectDataProvider>
            <Style x:Key="CellEditStyle" TargetType="{x:Type TextBox}">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Padding" Value="0"/>
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="ToolTip"
                                Value="{Binding RelativeSource={RelativeSource Self},
                                        Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </UserControl.Resources>
        <ScrollViewer>
            <DockPanel LastChildFill="True" Margin="0" VerticalAlignment="Top">
                <DataGrid x:Name="tagGrid" AutoGenerateColumns="False" ItemsSource="{Binding ElementName=dataGrid,Path=SelectedItem.UITag}" CanUserAddRows="True" CanUserDeleteRows="True" DockPanel.Dock="Right">
                    <DataGrid.Columns>
                        <DataGridComboBoxColumn Header="Tag Associati"  ItemsSource="{Binding Source={StaticResource TagData}}" Width="200" SelectedItemBinding="{Binding UITag}" DisplayMemberPath="{Binding Path=/Label}" SelectedValueBinding="{Binding Path=/Id}"></DataGridComboBoxColumn>
                    </DataGrid.Columns>
                </DataGrid>
                <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Source={StaticResource DeviceData}}" CanUserAddRows="True" CanUserDeleteRows="True" HorizontalAlignment="Left">
                    <DataGrid.RowValidationRules>
                        <DataErrorValidationRule ValidatesOnTargetUpdated="True" ValidationStep="UpdatedValue" />
                    </DataGrid.RowValidationRules>
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="ID" Binding="{Binding Id, ValidatesOnExceptions=True}" Width="100" EditingElementStyle="{StaticResource CellEditStyle}"></DataGridTextColumn>
                        <DataGridTextColumn Header="Label" Binding="{Binding Label, ValidatesOnExceptions=True}" Width="100" EditingElementStyle="{StaticResource CellEditStyle}"></DataGridTextColumn>
                        <DataGridComboBoxColumn Header="Tipo" SelectedValueBinding="{Binding Tipo, ValidatesOnExceptions=True}" Width="100">
                            <DataGridComboBoxColumn.ItemsSource>
                                <CompositeCollection>
                                    <system:String>Smartphone</system:String>
                                    <system:String>Totem</system:String>
                                </CompositeCollection>
                            </DataGridComboBoxColumn.ItemsSource>
                        </DataGridComboBoxColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </DockPanel>
        </ScrollViewer>
    
    </UserControl>
    

0 个答案:

没有答案