如果Binding为NULL,则Datagrid中的WPF ComboBox会引发错误 [英] WPF ComboBox in Datagrid throws error if Binding is NULL

查看:144
本文介绍了如果Binding为NULL,则Datagrid中的WPF ComboBox会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我要编辑的DataGrid。一列是一个组合框

 < DataGrid ItemsSource ={Binding Persons,Mode = TwoWay}
SelectedItem = {Binding SelectedPerson,Mode = TwoWay}>
< DataGrid.Columns>
< DataGridTemplateColumn Header =Company>
< DataGridTemplateColumn.CellEditingTemplate>
< DataTemplate>
< ComboBox ItemsSource ={Binding RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}},Path = DataContext.Companies}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellEditingTemplate>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< TextBlock Text ={Binding Company.Name}/>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< /DataGrid.Columns>
< / DataGrid>

问题是如果公司是 null 然后我得到错误


双向绑定需要路径或XPath


如何解决这个问题,让公司被设置为null?

解决方案

已经有了正确的答案,所以这只是一个快速跟进。
我们假设一个演示ViewModel:

  public MyViewModel()
{
CompanyType companyA = new CompanyType();
companyA.Name =Comp。A;
CompanyType companyB = new CompanyType();
companyB.Name =Comp。B;
Companies.Add(companyA);
Companies.Add(companyB);
PersonType person1 = new PersonType();
person1.Id = 1;
person1.Company = null;
PersonType person2 = new PersonType();
person2Id = 2;
person2.Company = companyB;
persons.Add(person1);
persons.Add(person2);
}

现在,ComboBox中不起作用的是它不会更改从ComboBox中选出不同公司的新价值后,个人公司的价值:在演示中,我们要从用户界面将company1和2分配给companyA。



此外,如果我们不绑定 SelectedItem 不 c $ c>该PersonType的公司属性的组合框。



您可以在此找到组合框架的Xaml的修复程序

 < DataTemplate> 
< ComboBox
DisplayMemberPath =Name
SelectedValuePath =Name
SelectedItem ={Binding Company}
ItemsSource ={Binding RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}},Path = DataContext.Companies}/>
< / DataTemplate>

我的演示课程

  public class PersonType //不需要在下面通知
{
public int Id {get;组; }
public CompanyType公司{get;组; }
}
public class CompanyType
{
public string Name {get;组; }
}

请注意,因为组合框 SelectedItem 的绑定,person2在开始时具有其ComboBox项目CompanyB 选择,现在可以将某个公司更改为CompanyA。


I have a DataGrid I want to edit. One column is a Combobox

<DataGrid ItemsSource="{Binding Persons, Mode=TwoWay}"
          SelectedItem="{Binding SelectedPerson, Mode=TwoWay}" >
     <DataGrid.Columns>
         <DataGridTemplateColumn Header="Company" >
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Companies}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Company.Name}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

The problem is if the Company is null then I get the error

Two-way binding requires Path or XPath

How can I get around that to allow the Company be set to null?

解决方案

The question already has a correct answer, so this is just a quick follow up. Let's assume a demo ViewModel:

public MyViewModel()
{
    CompanyType companyA = new CompanyType();
    companyA.Name = "Comp. A";
    CompanyType companyB = new CompanyType();
    companyB.Name = "Comp. B";
    Companies.Add(companyA);
    Companies.Add(companyB);
    PersonType person1 = new PersonType();
    person1.Id = 1;
    person1.Company = null;
    PersonType person2 = new PersonType();
    person2.Id = 2;
    person2.Company = companyB;
    persons.Add(person1);
    persons.Add(person2);
}

Now, what is not working in the ComboBox is that it is not changing the value of a Person's Company after a new value of a different Company is selected from the ComboBox: in the demo let's say we want assign both persons1 and 2 to companyA from the UI.

Furthermore CompanyB is not selected in the ComboBox of person2, if we don't bind the SelectedItem of the ComboBox to the Company property of the PersonType.

Here you find the fix to the Xaml of the Combobox

                <DataTemplate>
                    <ComboBox
                        DisplayMemberPath="Name"
                        SelectedValuePath="Name"
                        SelectedItem="{Binding Company}"
                        ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Companies}"/>
                </DataTemplate>

The classes of my demo

public class PersonType // don't need to notify below
{
    public int Id { get; set; }
    public CompanyType Company { get; set; }
}
public class CompanyType
{
    public string Name { get; set; }
}

Please notice also that, due to the binding of Combobox SelectedItem, person2 has its ComboBox item CompanyB selected in the beginning and now it is possible to change one's Company to CompanyA, for example.

这篇关于如果Binding为NULL,则Datagrid中的WPF ComboBox会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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