我是wpf的新手。尝试在数据网格中添加列表框。一切都运行完美,但选定的值绑定不起作用。它没有写回SelectedValue。请帮忙。
<DataGrid Name="SoruDataGrid" ItemsSource="{Binding Test.TestSonucuCollection}" Grid.Row="1" AutoGenerateColumns="False" Grid.ColumnSpan="2" >
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Soru.Id}"/>
<DataGridTextColumn Header="Soru" Binding="{Binding Soru.Text}" Width="300">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="AcceptsReturn" Value="true" />
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<DataGridTemplateColumn Header="Cevap">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox SelectionMode="Single"
ItemsSource="{Binding Soru.CevapCollection}"
DisplayMemberPath="Text"
SelectedValuePath="{Binding Id}"
SelectedValue="{Binding CevapId, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}">
CevapModel类
using System.ComponentModel;
namespace Test.Model
{
public class CevapModel : INotifyPropertyChanged
{
Cevap _cevap;
public event PropertyChangedEventHandler PropertyChanged;
public CevapModel()
{
_cevap = new Cevap();
}
public CevapModel(Cevap cevap)
{
_cevap = cevap;
}
public int Id
{
get { return _cevap.Id; }
set
{
_cevap.Id = value;
OnPropertyChanged("Id");
}
}
public int SoruId
{
get { return _cevap.SoruId; }
set
{
_cevap.SoruId = value;
OnPropertyChanged("SoruId");
}
}
public string Text
{
get { return _cevap.Text; }
set
{
_cevap.Text = value;
OnPropertyChanged("Text");
}
}
public int Puan
{
get { return _cevap.Puan; }
set
{
_cevap.Puan = value;
OnPropertyChanged("Puan");
}
}
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
TestSonucuModel.cs
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
namespace Test.Model
{
public class TestSonucuModel : INotifyPropertyChanged
{
TestSonucu _testSonucu;
SoruModel _soru;
public event PropertyChangedEventHandler PropertyChanged;
public TestSonucuModel()
{
_testSonucu = new TestSonucu();
}
public TestSonucuModel(TestSonucu testSonucu)
{
_testSonucu = testSonucu;
}
public int Id
{
get { return _testSonucu.Id; }
set
{
_testSonucu.Id = value;
OnPropertyChanged("Id");
}
}
public int TestId
{
get { return _testSonucu.TestId; }
set
{
_testSonucu.TestId = value;
OnPropertyChanged("TestId");
}
}
public int SoruId
{
get { return _testSonucu.SoruId; }
set
{
_testSonucu.SoruId = value;
OnPropertyChanged("SoruId");
}
}
public SoruModel Soru
{
get { return _soru; }
set
{
_soru = value;
OnPropertyChanged("Soru");
}
}
public int CevapId
{
get { return _testSonucu.CevapId; }
set
{
_testSonucu.CevapId = value;
OnPropertyChanged("CevapId");
}
}
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public static implicit operator TestSonucu(TestSonucuModel t)
{
return t._testSonucu;
}
}
}}
答案 0 :(得分:0)
正如您所说,CevapId
是TestSonucu
类的属性,它是DataGrid
每行后面的视图模型,您不需要在此更改绑定上下文case,因为它将被设置为TestSonucu
class
<ListBox ...
ItemsSource="{Binding Soru.CevapCollection}"
DisplayMemberPath="Text"
SelectedValuePath="Id"
SelectedValue="{Binding Path=CevapId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
同样SelectedValuePath
应该只是Id
,与指定DisplayMemberPath
的方式相同,这意味着每个Text
项的CevapCollection
属性都是显示和Id
属性值