【问题标题】:wpf datagridcombobox not updating sourcewpf datagridcombobox不更新源
【发布时间】:2015-03-20 06:12:55
【问题描述】:

我在 WPF 中有一个绑定到可观察集合的 DataGrid。由于我需要监视哪些特定记录是“脏”的,因此我将数据类指定为 INotifiable 并将“IsDirty”属性设置为每个字段的设置器的一部分。

我的 DataGrid 有两个列,一个文本列和一个组合框列。当文本列更改时,会调用相应属性的设置器 - 但是当组合框更改时,不会调用该属性的设置器 - 有什么想法我哪里出错了吗?

编辑:我要指出数据最初加载正确,并且我能够更新 UI 中的组合框。此外,每当对网格进行任何更改时,它都会出现红色边框,就像绑定失败一样。但是,当我更改名称列时它也会得到这个,并且工作正常,所以不确定是否链接

编辑:我还尝试添加一个选择更改事件以查看它是否会触发并且确实如此。但是,setter 顽固地不会被调用,因此我的基础数据不会得到更新

查看模型:

public class ContractConfigViewModel : INotifyPropertyChanged
{

    public class ClientSurveyor: INotifyPropertyChanged
    {
        public ClientSurveyor()
        {
            IsDirty = false;
        }

        public bool IsDirty;

        private ZoomLineManager LineManager;
        public ZoomLineManager linemanager
        {
            get
            {
                return LineManager;
            }
            set
            {
                LineManager = value;
                IsDirty = true;
                OnPropertyChanged("linemanager");
            }
        }

        private string Name;
        public string name
        {
            get
            {
                return Name;
            }
            set
            {
                Name = value;
                IsDirty = true;
                OnPropertyChanged("name");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string PropertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    }

    private ObservableCollection<ClientSurveyor> allClientReps;
    public ObservableCollection<ClientSurveyor> allclientreps
    {
        get
        {
            return allClientReps;
        }
        set
        {
            allClientReps = value;
            OnPropertyChanged("allclientreps");
        }
    }

    private ObservableCollection<ZoomLineManager> allLineManagers;
    public ObservableCollection<ZoomLineManager> alllinemanagers
    {
        get
        {
            return allLineManagers;
        }
        set
        {
            allLineManagers = value;
            OnPropertyChanged("alllinemanagers");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string PropertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }
}

WPF:

<DataGrid Margin="5" Grid.ColumnSpan="4" ItemsSource="{Binding Path=allclientreps}" SelectedItem="{Binding Path=selectedclientrep}" AutoGenerateColumns="False">
<DataGrid.Columns>
    <DataGridTextColumn Header="Representative Name" Width="*" Binding="{Binding Path=name, NotifyOnTargetUpdated=True}"/>

    <DataGridComboBoxColumn Header="Line Manager" Width="*" 
                            SelectedValueBinding="{Binding linemanager.ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                            SelectedValuePath="ID" 
                            DisplayMemberPath="Name"
                            >
        <DataGridComboBoxColumn.ElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.alllinemanagers}"/>
            </Style>
        </DataGridComboBoxColumn.ElementStyle>
        <DataGridComboBoxColumn.EditingElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.alllinemanagers}"/>     
            </Style>
        </DataGridComboBoxColumn.EditingElementStyle>
    </DataGridComboBoxColumn>

</DataGrid.Columns>

【问题讨论】:

  • 为什么要使用 ElementStyle 和 EditingElementStyle?为什么不在 DataGridComboBoxColumn 上设置 ItemsSource?
  • 您是否为 ZoomLineManager 实现了 INotifyPropertyChanged?作为旁注,我建议查看 PropertyChanged.Fody (nuget.org/packages/PropertyChanged.Fody)

标签: c# wpf datagrid combobox


【解决方案1】:

您需要在 ViewModel 上为选定的客户端代表创建一个属性。

private ClientSurveyor selectedclientrep;
public ClientSurveyor SelectedClientRep
{
    get { return selectedclientrep; }
    set
    {
        selectedclientrep = value;
        OnPropertyChanged("SelectedClientRep");
    }
}

您还需要将 View 上的 SelectedItem 绑定到新属性 SelectedClientRep。

<DataGrid Margin="5" Grid.ColumnSpan="4" ItemsSource="{Binding Path=allclientreps}" SelectedItem="{Binding Path=SelectedClientRep}" AutoGenerateColumns="False">

我希望这会有所帮助。

【讨论】:

  • 嗨 Natan,抱歉,这不起作用。我的视图模型和绑定中已经有了 SelectedClientRep 属性和字段。我不小心错过了我发布的代码。
猜你喜欢
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
  • 1970-01-01
相关资源
最近更新 更多