【问题标题】:Datagrid Textboxes Values not updated with ObservableCollection数据网格文本框值未使用 ObservableCollection 更新
【发布时间】:2020-04-08 10:13:01
【问题描述】:

我在ViewModel 中有一个ObservableCollection<T>

private ObservableCollectionX<SSYLine> _ssyLines = new ObservableCollectionX<SSYLine>();
    public ObservableCollectionX<SSYLine> SsyLines
    {
        get { return _ssyLines; }
        set
        {
            if (Setter(ref _ssyLines, value, nameof(SsyLines)))
            {

            }
        }
    }

所以我添加了一个像 this 这样的新行:

public void AddNewLine(SSYLine line)
    {
        if (IsEnabled)
        {
            line.PropertyChanged += LinePropertyChangedHandler;                

            SsyLines.Add(line);
            SelectedLine = SsyLines[SsyLines.Count - 1];
        }
    }

类 (SSYLine) 上的 PropertyChanged:

public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
            }
        }

我的XAML 代码:

<DataGrid ItemsSource="{Binding SsyLines, Mode=TwoWay}" x:Name="SsyDatagrid" Grid.Row="1" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserAddRows="False"
              SelectionChanged="LineSelectionChangedHandler"
              HeadersVisibility="Column"
              SelectedItem="{Binding SelectedLine, Mode=TwoWay}"
              GridLinesVisibility="None" SourceUpdated="SsyDatagrid_SourceUpdated" BorderThickness="0" SelectionMode="Single" CanUserResizeColumns="False" CanUserResizeRows="False" Background="Transparent" AutoGenerateColumns="False" Margin="0,9,0,0" 
              CurrentCellChanged="LinesDatagrid_CurrentCellChanged">

文本框:

<DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox x:Name="LineType" Width="Auto"
                                 Text="{Binding detailType, Mode=TwoWay}"
                                 LostFocus="TextBoxLostFocus"
                                    GotKeyboardFocus="TextBoxGotKeyboardFocusHandler"
                         HorizontalContentAlignment="Right" >
                            <TextBox.Style>
                                <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                                    <Style.Triggers>
                                        <DataTrigger
                                 Binding="{Binding ExplicitValidations,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}"
                                            Value="True"  >
                                            <Setter Property="Background" Value="LightGreen"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

稍后,我从database 获取一些数据,并在ObservableCollection 中为每条记录添加一行。 我有4条记录。正确创建了 4 行,但没有一个 textboxes 有值。 ObservableCollection 是正确的,具有正确的数据。该问题仅在 TextBox 值上仍然存在。

我在这里错过了什么?

谢谢。

【问题讨论】:

  • 输出控制台是否出现绑定错误?
  • 是的,我得到以下输出:System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“SSYLine”(HashCode=46769243)上找不到“detailType”属性。绑定表达式:路径=detailType; DataItem='SSYLine' (HashCode=46769243);目标元素是'TextBox'(名称='LineType');目标属性是“文本”(类型“字符串”)
  • 属性detailType在哪里定义?它显然不是SSYLine 的成员。另请记住,您不能绑定到字段。 detailType 必须是公共属性。
  • 经过进一步调查发现,detailType在类中被声明为DetailType。谢谢大家!

标签: c# wpf xaml data-binding datagrid


【解决方案1】:

在“SSYLine”类中,您必须声明 detailType 属性:

public string DetailType {set; get;}

如果它是一个字符串 ofc。

它应该以大写开头。

【讨论】:

  • 是的,你是对的!我发现 detailType 在类中被声明为 DetailType。非常感谢。
猜你喜欢
  • 2018-07-05
  • 2016-04-20
  • 2019-11-14
  • 2011-01-25
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 2011-02-15
相关资源
最近更新 更多