选定值绑定不会写回

本文关键字:写回 绑定 | 更新日期: 2023-09-27 18:03:42

我是wpf的新手。尝试在数据网格中添加列表框。一切运行良好,但所选值绑定不起作用。它不会写回选定值。请帮忙。

<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}}}">

Cevap模型类

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));
            }
        }
    }
}

测试Sonucu模型.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;
        }
    }
}}

选定值绑定不会写回

如果,如您所说,CevapIdTestSonucu类的属性,它是DataGrid每行背后的视图模型,在这种情况下您不需要更改绑定上下文,因为它将设置为TestSonucu类的实例

<ListBox ...
    ItemsSource="{Binding Soru.CevapCollection}" 
    DisplayMemberPath="Text" 
    SelectedValuePath="Id"
    SelectedValue="{Binding Path=CevapId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

此外,SelectedValuePath应该只是Id,就像您指定DisplayMemberPath一样,这意味着它将采用每个CevapCollectionText属性进行显示,并将Id属性作为值