组合框在数据网格中的绑定给出了错误 [英] Binding of combobox within datagrid is giving error

查看:57
本文介绍了组合框在数据网格中的绑定给出了错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我查看如下



< window x:class = TestWPFApplication.MainWindowxmlns:x =#unknown>

xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:testns =clr-namespace:TestWPFApplication.ViewModel

xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml

Title =MainWindowHeight =350Width =525

>

< window.datacontext>

< ; testns:testviewmodel x:name =_ testViewModelid =1name =Abdulxmlns:testns =#unknown>

< / testns:testviewmodel>

< /window.datacontext>

< grid>

< Label Content =输入名称Horizo​​ntalAlignment =LeftMargin =58, 62,0,0VerticalAlignment =TopName =lblName/>

< textbox horizo​​ntalalignment =leftheight =22margin =160,66,0,0 textwrapping =Wra p>

Name =txtNameVerticalAlignment =TopWidth =120Text ={Binding Name,Mode = TwoWay,ValidatesOnDataErrors = True}

/>

<! - < Button Content =使用一个参数保存Horizo​​ntalAlignment =LeftMargin =69,142,0,0VerticalAlignment =TopWidth = 159Name =btnSaveSpecific

Command ={Binding Path = SaveWithOneParamenter}

CommandParameter ={Binding ElementName = txtName,Path = Text}

RenderTransformOrigin = - 1.855,0.516/> - >

< Button Content =使用一个参数保存Horizo​​ntalAlignment =LeftMargin =69,142,0 ,0VerticalAlignment =TopWidth =159Name =btnSaveSpecific

Command ={Binding Path = ClickCommandSaveWithOneParamenter}

RenderTransformOrigin = - 1.855, 0.516/>

< Label Content =Enter IdHorizo​​ntalAlignment =LeftMargin =58,17,0,0VerticalAlignment =TopName =lblId/>

< textbox text ={Binding Id,Mode = TwoWay,ValidatesOnDataErrors = True}horizo​​ntalalignment =Leftheight =22margin =160,20,0,0textwrapping =Wrap> ;

x:Name =txtIdVerticalAlignment =TopWidth =120IsReadOnlyCaretVisible =True/>

< Button Content =参数Horizo​​ntalAlignment =LeftMargin =253,142,0,0VerticalAlignment =TopWidth =163Name =btnSaveSpecific2

Command ={Binding Path = ClickCommand}< br $> b $ b />

< datagrid horizo​​ntalalignment =Leftmargin =103,188,0,0verticalalignment =Topheight =98width =313 >

IsSynchronizedWithCurrentItem =TrueItemsSource ={Binding Path = PersonNames,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}

SelectedItem ={Binding SelectedItem,UpdateSourceTrigger = PropertyChanged,Mode = TwoWay}

AutoGenerateColumns =False>

< datagrid.columns>

< datagridtextcolumn header =Employee Idbinding ={Binding Path = Id,UpdateSourceTrigger = PropertyChanged,Mode = TwoWay}/>

< datagridtextcolumn header =员工姓名binding ={Binding Path = Name,UpdateSourceTrigger = PropertyChanged,Mode = TwoWay}/>

< datagridtemplatecolumn header =Statuswidth =100>

< datagridtemplatecolumn.celltemplate>

< datatemplate>

< textblock text ={Binding SelectedStatus.StatusName}>< / textblock>

< / datatemplate>

< /datagridtemplatecolumn.celltemplate>

< datagridtemplatecolumn.celleditingtemplate>

< datatemplate>

< combobox height =22> ItemsSource ={StaticResource ListOfStatuses }

SelectedItem ={Binding SelectedStatus}>< / combobox>

< / datatemplate>

< /datagridtemplatecolumn.celleditingtemplate>

< / datagridtemplatecolumn>



< /datagrid.columns>

< / datagrid>

< Label Content ={Binding SelectedItem.Id}Name =lblSelectedIdHorizo​​ntalAlignment =LeftMargin =130,294,0,0VerticalAlignment =Top />

< Label Content ={Binding SelectedItem.Name}Name =lblSelectedNameHorizo​​ntalAlignment =LeftMargin =271,294,0,0VerticalAlignment =To p/>

< / textbox>< / textbox>< / grid>

< / window>



然后我有如下的ViewModel

公共类TestViewModel:INotifyPropertyChanged,IDataErrorInfo

{

TestData _testData;

ObservableCollection< status> _listOfStatuses;





// public TestCommand TestCommand {get;组; } $ / $
public SaveWithOneParamenter SaveWithOneParamenter {get;组; } $ / $
私人SavingItemDetailsViewModel _savingItemDetailsViewModel;



公共TestViewModel()

{

_testData = new TestData();

_personNames = new ObservableCollection< testdata>();

// _ dictionaryNames = new ObservableCollection< tuple>< int,>> ();



//this.TestCommand = new TestCommand(this);

this.SaveWithOneParamenter = new SaveWithOneParamenter(this);



_savingItemDetailsViewModel = new SavingItemDetailsViewModel();



_listOfStatuses = new ObservableCollection< status>();

_listOfStatuses.Add(new Status(1,Active));

_listOfStatuses。添加(新状态(2,InActive));

_listOfStatuses.Add(新状态(3,已删除));



}



private int _id;

public int Id

{

得到{return _id; }

设定

{

_id = value; NotifyPropertyChanged(Id);

}

}

private string _name;

public string Name

{

get {return _name; }

set

{

_name = value;



NotifyPropertyChanged(姓名);

}

}



私人TestData TestData;

public TestData SelectedItem

{get {return TestData; } set {TestData = value; NotifyPropertyChanged( 的SelectedItem); } $



私人状态_status;

公共状态SelectedStatus

{get {return _status; } set {_status = value; NotifyPropertyChanged( SelectedStatus); } $



public ObservableCollection< status> ListOfStatuses

{

get {return _listOfStatuses; }

private set {_listOfStatuses = value; NotifyPropertyChanged( ListOfStatuses); }

}







ObservableCollection< testdata> _personNames;

public ObservableCollection< testdata> PersonNames

{

get {return _personNames; }

设定

{

_personNames = value; NotifyPropertyChanged( PersonNames);

}

}



ObservableCollection< tuple>< int,>> _dictionaryNames;

ObservableCollection< tuple>< int,>> DictionaryNames

{

get {return _dictionaryNames; }

设定

{

_dictionaryNames = value; NotifyPropertyChanged(DictionaryNames);

}

}



公共事件PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName)

{

PropertyChangedEventHandler handler = PropertyChanged;

if(null!= handler)

{

handler(this,new PropertyChangedEventArgs(propertyName));

}

}



私人ICommand _clickCommand;

公共ICommand ClickCommandSaveWithTwoParamenters

{

get

{

返回_clickCommand ?? (_clickCommand = new SaveWithTwoParamenters(()=> AddAnItem(),CanExecute));

}

}



public ICommand ClickCommandSaveWithOneParamenter

{

get

{

return _clickCommand ?? (_clickCommand = new SaveWithTwoParamenters(()=> AddAnItem(Name),CanExecute));

}

}



public bool CanExecute {get {return string.IsNullOrEmpty(Error); } private set {}}

public void AddAnItem()

{

SavingItemDetails _savingItemDetails = new SavingItemDetails();

_savingItemDetails.DataContext = _savingItemDetailsViewModel;



_savingItemDetailsViewModel.SavingId = Id;

_savingItemDetailsViewModel.SavingName = _name;

_savingItemDetailsViewModel.SaveMassage =Saving Id =+ Id.ToString()+,Name =+ _name;



_savingItemDetails.ShowDialog();



_personNames.Add(new TestData(Id,_ name));

}



public void AddAnItem(string _name)

{

SavingItemDetails _savingItemDetails = new SavingItemDetails();

_savingItemDetails.DataContext = _savingItemDetailsViewModel;



_savingItemDetai lsViewModel.SavingId = Id;

_savingItemDetailsViewModel.SavingName = _name;

_savingItemDetailsViewModel.SaveMassage =Saving Id =+ Id.ToString()+,Name =+ _name;



_savingItemDetails.ShowDialog();



int t;



if((_personNames == null)|| (_personNames.Count()< = 0))

t = 1;

else

t =(_ personNames.Count< = 0) ? 1:_personNames.Max(x => x.Id)+ 1;



_personNames.Add(new TestData(t,_ name));



// int t;



// if((_personNames == null)||(_personNames.Count( )< = 0))

// t = 1;

//其他

// t =(_ personNames.Count< = 0)? 1:_personNames.Max(x => x.Id)+ 1;



// SavingItemDetails _savingItemDetails = new SavingItemDetails();

//_savingItemDetails.DataContext = _savingItemDetailsViewModel;



//_savingItemDetailsViewModel.SavingId = t;

//_savingItemDetailsViewModel.SavingName = _name;

//_savingItemDetailsViewModel.SaveMassage =Saving Id =+ t.ToString()+,Name =+ _name;



/ /_savingItemDetails.ShowDialog();



//_personNames.Add(new TestData(t,_ name));

}



公共字符串错误

{

get;

私人套装;

}



公共字符串这个[string columnName]

{

get

{

if(colu mnName ==Id)

{

if(Id == 0)

错误=Id不能为0; < br $>
}



if(columnName ==Name)

{

if(string.IsNullOrEmpty(Name))

错误=名字不能为空;

}



返回错误;

}

}



}



然后我的模型如下

公共类状态:INotifyPropertyChanged,IDataErrorInfo

{

private int _statusId;

私有字符串_statusName;



public int StatusId {get {return _statusId; } set {_statusId = value; NotifyPropertyChanged( StatusId); } $

公共字符串StatusName {get {return _statusName; } set {_statusName = value; NotifyPropertyChanged( StatusName); } $



public Status(int _statusId,string _statusName)

{

this.StatusId = _statusId;

this.StatusName = _statusName;

}



公共事件PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName)

{

PropertyChangedEventHandler handler = PropertyChanged;

if(null!= handler)

{

handler(this,new PropertyChangedEventArgs(propertyName));

}

}





公共字符串错误

{

get;

private set; < br $>
}



public string this [string columnName]

{

get

{

if(columnName ==StatusId)

{

if(StatusId == 0)

错误=StatusId不能为0;

}



if(columnName ==StatusName)

{

if(string.IsNullOrEmpty(StatusName))

错误=StatusName不能为空;

}



返回错误;

}

}

}



错误在粗体文本处:ItemsSource ={StaticResource ListOfStatuses}



并且它显示Item ListOfStatuses can不能解决,甚至ListOfStatuses都存在于ViewModel中。有人可以在这方面帮助我。在此先感谢。

我尝试将它放在模型中,该模型与DataData的TestData正确绑定,因为我尝试过,仍然没有解决任何帮助,我非常感激。



我尝试过的事情:



我正在努力尝试不同的组合和搜索以及

Hi All,

I have View as below

<window x:class="TestWPFApplication.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:testns="clr-namespace:TestWPFApplication.ViewModel"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
>
<window.datacontext>
<testns:testviewmodel x:name="_testViewModel" id="1" name="Abdul" xmlns:testns="#unknown">
</testns:testviewmodel>
</window.datacontext>
<grid>
<Label Content="Enter Name" HorizontalAlignment="Left" Margin="58,62,0,0" VerticalAlignment="Top" Name="lblName"/>
<textbox horizontalalignment="Left" height="22" margin="160,66,0,0" textwrapping="Wrap">
Name="txtName" VerticalAlignment="Top" Width="120" Text="{Binding Name, Mode=TwoWay, ValidatesOnDataErrors=True}"
/>
<!--<Button Content="Save with One Parameter" HorizontalAlignment="Left" Margin="69,142,0,0" VerticalAlignment="Top" Width="159" Name="btnSaveSpecific"
Command="{Binding Path=SaveWithOneParamenter}"
CommandParameter="{Binding ElementName=txtName, Path=Text}"
RenderTransformOrigin="-1.855,0.516" />-->
<Button Content="Save with One Parameter" HorizontalAlignment="Left" Margin="69,142,0,0" VerticalAlignment="Top" Width="159" Name="btnSaveSpecific"
Command="{Binding Path=ClickCommandSaveWithOneParamenter}"
RenderTransformOrigin="-1.855,0.516" />
<Label Content="Enter Id" HorizontalAlignment="Left" Margin="58,17,0,0" VerticalAlignment="Top" Name="lblId"/>
<textbox text="{Binding Id, Mode=TwoWay, ValidatesOnDataErrors=True}" horizontalalignment="Left" height="22" margin="160,20,0,0" textwrapping="Wrap">
x:Name="txtId" VerticalAlignment="Top" Width="120" IsReadOnlyCaretVisible="True" />
<Button Content="Save with Tow Parameter" HorizontalAlignment="Left" Margin="253,142,0,0" VerticalAlignment="Top" Width="163" Name="btnSaveSpecific2"
Command="{Binding Path=ClickCommand}"
/>
<datagrid horizontalalignment="Left" margin="103,188,0,0" verticalalignment="Top" height="98" width="313">
IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=PersonNames, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedItem, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
AutoGenerateColumns="False" >
<datagrid.columns>
<datagridtextcolumn header="Employee Id" binding="{Binding Path=Id, UpdateSourceTrigger=PropertyChanged,Mode =TwoWay}" />
<datagridtextcolumn header="Name of Employee" binding="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
<datagridtemplatecolumn header="Status" width="100">
<datagridtemplatecolumn.celltemplate>
<datatemplate>
<textblock text="{Binding SelectedStatus.StatusName}"></textblock>
</datatemplate>
</datagridtemplatecolumn.celltemplate>
<datagridtemplatecolumn.celleditingtemplate>
<datatemplate>
<combobox height="22">ItemsSource="{StaticResource ListOfStatuses}"
SelectedItem="{Binding SelectedStatus}"></combobox>
</datatemplate>
</datagridtemplatecolumn.celleditingtemplate>
</datagridtemplatecolumn>

</datagrid.columns>
</datagrid>
<Label Content="{Binding SelectedItem.Id}" Name="lblSelectedId" HorizontalAlignment="Left" Margin="130,294,0,0" VerticalAlignment="Top"/>
<Label Content="{Binding SelectedItem.Name}" Name="lblSelectedName" HorizontalAlignment="Left" Margin="271,294,0,0" VerticalAlignment="Top"/>
</textbox></textbox></grid>
</window>

Then I have ViewModel as below
public class TestViewModel : INotifyPropertyChanged, IDataErrorInfo
{
TestData _testData;
ObservableCollection<status> _listOfStatuses;


//public TestCommand TestCommand { get; set; }
public SaveWithOneParamenter SaveWithOneParamenter { get; set; }
private SavingItemDetailsViewModel _savingItemDetailsViewModel;

public TestViewModel()
{
_testData = new TestData();
_personNames = new ObservableCollection<testdata>();
//_dictionaryNames = new ObservableCollection<tuple><int,>>();

//this.TestCommand = new TestCommand(this);
this.SaveWithOneParamenter = new SaveWithOneParamenter(this);

_savingItemDetailsViewModel = new SavingItemDetailsViewModel();

_listOfStatuses = new ObservableCollection<status>();
_listOfStatuses.Add(new Status(1, "Active"));
_listOfStatuses.Add(new Status(2, "InActive"));
_listOfStatuses.Add(new Status(3, "Deleted"));

}

private int _id;
public int Id
{
get { return _id; }
set
{
_id = value; NotifyPropertyChanged("Id");
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;

NotifyPropertyChanged("Name");
}
}

private TestData TestData;
public TestData SelectedItem
{ get { return TestData; } set { TestData = value; NotifyPropertyChanged("SelectedItem"); } }

private Status _status;
public Status SelectedStatus
{ get { return _status; } set { _status = value; NotifyPropertyChanged("SelectedStatus"); } }

public ObservableCollection<status> ListOfStatuses
{
get { return _listOfStatuses; }
private set { _listOfStatuses = value; NotifyPropertyChanged("ListOfStatuses"); }
}



ObservableCollection<testdata> _personNames;
public ObservableCollection<testdata> PersonNames
{
get { return _personNames; }
set
{
_personNames = value; NotifyPropertyChanged("PersonNames");
}
}

ObservableCollection<tuple><int,>> _dictionaryNames;
ObservableCollection<tuple><int,>> DictionaryNames
{
get { return _dictionaryNames; }
set
{
_dictionaryNames = value; NotifyPropertyChanged("DictionaryNames");
}
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}

private ICommand _clickCommand;
public ICommand ClickCommandSaveWithTwoParamenters
{
get
{
return _clickCommand ?? (_clickCommand = new SaveWithTwoParamenters(() => AddAnItem(), CanExecute));
}
}

public ICommand ClickCommandSaveWithOneParamenter
{
get
{
return _clickCommand ?? (_clickCommand = new SaveWithTwoParamenters(() => AddAnItem(Name), CanExecute));
}
}

public bool CanExecute { get { return string.IsNullOrEmpty(Error); } private set { } }
public void AddAnItem()
{
SavingItemDetails _savingItemDetails = new SavingItemDetails();
_savingItemDetails.DataContext = _savingItemDetailsViewModel;

_savingItemDetailsViewModel.SavingId = Id;
_savingItemDetailsViewModel.SavingName = _name;
_savingItemDetailsViewModel.SaveMassage = "Saving Id=" + Id.ToString() + ", Name=" + _name;

_savingItemDetails.ShowDialog();

_personNames.Add(new TestData(Id, _name));
}

public void AddAnItem(string _name)
{
SavingItemDetails _savingItemDetails = new SavingItemDetails();
_savingItemDetails.DataContext = _savingItemDetailsViewModel;

_savingItemDetailsViewModel.SavingId = Id;
_savingItemDetailsViewModel.SavingName = _name;
_savingItemDetailsViewModel.SaveMassage = "Saving Id=" + Id.ToString() + ", Name=" + _name;

_savingItemDetails.ShowDialog();

int t;

if ((_personNames == null) || (_personNames.Count() <= 0))
t = 1;
else
t = (_personNames.Count <= 0) ? 1 : _personNames.Max(x => x.Id) + 1;

_personNames.Add(new TestData(t, _name));

//int t;

//if ((_personNames == null) || (_personNames.Count() <= 0))
// t = 1;
//else
// t = (_personNames.Count <= 0) ? 1 : _personNames.Max(x => x.Id) + 1;

//SavingItemDetails _savingItemDetails = new SavingItemDetails();
//_savingItemDetails.DataContext = _savingItemDetailsViewModel;

//_savingItemDetailsViewModel.SavingId = t;
//_savingItemDetailsViewModel.SavingName = _name;
//_savingItemDetailsViewModel.SaveMassage = "Saving Id=" + t.ToString() + ", Name=" + _name;

//_savingItemDetails.ShowDialog();

//_personNames.Add(new TestData(t, _name));
}

public string Error
{
get;
private set;
}

public string this[string columnName]
{
get
{
if (columnName == "Id")
{
if (Id == 0)
Error = "Id Can not be 0";
}

if (columnName == "Name")
{
if (string.IsNullOrEmpty(Name))
Error = "Name can not be empty";
}

return Error;
}
}

}

Then I have Model as below
public class Status : INotifyPropertyChanged, IDataErrorInfo
{
private int _statusId;
private string _statusName;

public int StatusId { get { return _statusId; } set { _statusId = value; NotifyPropertyChanged("StatusId"); } }
public string StatusName { get { return _statusName; } set { _statusName = value; NotifyPropertyChanged("StatusName"); } }

public Status(int _statusId, string _statusName)
{
this.StatusId = _statusId;
this.StatusName = _statusName;
}

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}


public string Error
{
get;
private set;
}

public string this[string columnName]
{
get
{
if (columnName == "StatusId")
{
if (StatusId == 0)
Error = "StatusId Can not be 0";
}

if (columnName == "StatusName")
{
if (string.IsNullOrEmpty(StatusName))
Error = "StatusName can not be empty";
}

return Error;
}
}
}

The error is at the bold text that is: ItemsSource="{StaticResource ListOfStatuses}"

and it says "Item ListOfStatuses could not be resolved", even ListOfStatuses is there in the ViewModel. Can somebody please help me in this regards. Thanks in advance.
I tried by putting it within the Model that's being bound with the DataGrid that's TestData right, in that I tried putting, still not solved any help is much appreciated.

What I have tried:

I am trying my best by putting different combinations and searching as well

推荐答案

尝试在窗口中输入名称(例如WindowName)并更改

Try to give a Name (WindowName for example) to your window and change
ItemsSource="{StaticResource ListOfStatuses}"



to


to

ItemsSource="{Binding ElementName=WindowName, Path=ListOfStatuses, Mode=OneWay}"


这篇关于组合框在数据网格中的绑定给出了错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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