将DatagridColumn绑定到StaticResource,指向WPF中的ObservableCollection [英] Binding DatagridColumn to StaticResource pointing to ObservableCollection in WPF

查看:38
本文介绍了将DatagridColumn绑定到StaticResource,指向WPF中的ObservableCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GridView中,可以将selectedItemBinding路径设置为DataGrid.ItemsSource的属性,将ItemsSource列手动绑定到Enum,像这样:

It is possible in a GridView to manualy bind column ItemsSource to Enum with selectedItemBinding path set to to property of DataGrid.ItemsSource like this:

<Window.Resources>
<ObjectDataProvider x:Key="DirectionEnum"
                    MethodName="GetValues"
                    ObjectType="{x:Type core:Enum}">
        <ObjectDataProvider.MethodParameters>
                <x:Type Type="local:Direction"/>
        </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>

...

<DataGrid x:Name="DgvZlecNag" 
    AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridComboBoxColumn Header="Column0"
                 SelectedItemBinding="{Binding Direction}"
                 ItemsSource="{Binding Source={StaticResource DirectionEnum}}"/>

...

public enum Direction 
{
    Def = 0,
    Imp = 1,
    Exp = 2,
}

...

public MainWindow()
    {

        InitializeComponent();
        _orders = new ObservableCollection<ZlecNag>()
        {
            new ZlecNag() {
                Id = 1,
                Direction = Direction.Imp
                }
        }
    DgvZlecNag.ItemsSource = zlecenia;
    }

我想要做的是将列类似地绑定到ObservableCollection,但是它不太有效。
我尝试使用column1中的staticResource做到这一点,但可以正常工作,但不显示初始值集,
以及column2中的本地observableCollection,它显示初始值,但绑定属性的值不变在组合框中选择新项目。
Column3仅用于显示绑定属性是否发生更改。

What i'm trying to do is to similarly bind column to ObservableCollection but it doesn't quite work. I tried to do this with staticResource in column1, which works but doesn't show value initialy set, and to local observableCollection in column2, which shows the initial value but value of binded propery doesn't change with selecting new item in combobox. Column3 is just to show if the binded property changes.

<Window x:Class="ZleceniaTransportowe2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ZleceniaTransportowe2"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:core="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="500" Width="1200"
        >
<Window.Resources>
       <ObjectDataProvider x:Key="Clients"
                            MethodName="GetData"
                            ObjectType="{x:Type local:CollectionData}"
                            >
            <ObjectDataProvider.MethodParameters>
                <x:Type Type="local:CollectionData"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <local:IfNullConverter x:Key="IfNullConverter"/>
</Window.Resources>
        <DataGrid x:Name="DgvZlecNag" 
                  AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Id"
                                    Binding="{Binding Id, Mode=OneWay}"/>
                <DataGridComboBoxColumn Header="Column1"
                                        SelectedItemBinding="{Binding Path=Client, Mode=TwoWay}"
                                        ItemsSource="{Binding Source={StaticResource Clients}}"
                                        SelectedValuePath="Akronim"  DisplayMemberPath="Akronim"/>
                <DataGridTemplateColumn Header="Column2">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox SelectedValue="{Binding Client.Akronim, Mode=TwoWay}"
                                      DisplayMemberPath="Akronim"  SelectedValuePath="Akronim" SelectedItem="Client"
                                      ItemsSource= "{Binding Clients, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window }}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="column3"
                                    Binding="{Binding Client.Akronim, Mode=OneWay}"/>
            </DataGrid.Columns>

        </DataGrid>
</Window>

后面的代码:

namespace ZleceniaTransportowe2
{
    public partial class MainWindow: Window
    private ObservableCollection<ZlecNag> _orders;

    public ObservableCollection<Client> Clients {get;} = new ObservableCollection<Client>()
        {
            new Client()
            {
                Akronim = "Seifert",
            },
            new Client()
            {
                Akronim = "Cenergo",
            }
        };

        public MainWindow()
        {
            InitializeComponent();
            _orders = new ObservableCollection<ZlecNag>()
            {
                new ZlecNag() {
                    Id = 1,
                    Client = Clients.First(),

                },
               new ZlecNag() {
                    Id = 1,
                    Client = Clients[1],
                   }
                };
             DgvZlecNag.ItemsSource = _orders;
        }
    }
}

...

public class ZlecNag : INotifyPropertyChanged
{

    private Direction _direction;
    private Client _client;

    public Direction Direction
    {
        get { return _client; }
        set
        {
            _direction= value;
            OnPropertyChanged();
        }
    }

    public Client Client
    {
        get { return _client; }
        set
        {
            _client = value;
            OnPropertyChanged();
        }
    }
        public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

...

public class Client : INotifyPropertyChanged

{
    private int _gidNumer;
    private string _akronim;

    public int GidNumer
    {
        get { return _gidNumer; }
        set
        {
            _gidNumer = value;
            OnPropertyChanged();
        }
    }

    public string Akronim

    {
        get { return _akronim; }
        set
        {
            _akronim = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

...

具有列2的objectDataProvider数据的类:

Class with data for objectDataProvider for column2:

public class CollectionData
{
    public static ObservableCollection<Client> GetData(Type type = null)
    {
        var clients = new ObservableCollection<Client>()
        {
            new Client()
            {
                Akronim = "Seifert",
                GidNumer = 4654
            },
              new Kontrahent()
            {
                Akronim = "Cenergo",
                GidNumer = 4656
            }
        };
        return clients;
    }
}

请帮忙,我不知道如何解决此问题。

Please help, i out of ideas how to fix this.

推荐答案

您未在column2中使用绑定,而是编写了 SelectedItem = Client ,请考虑在SelectedItem中绑定。

You are not using Binding in column2, instead you have written SelectedItem="Client" , please consider binding in SelectedItem.

这篇关于将DatagridColumn绑定到StaticResource,指向WPF中的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆