【问题标题】:NumericUpDown value bindingNumericUpDown 值绑定
【发布时间】:2017-01-09 15:00:18
【问题描述】:

我有问题。如何将 ItemsSource 属性绑定到 NumericUpDown?这种方式是行不通的。谢谢!

<DataGrid ItemsSource="{Binding Articles}">
    <DataGrid.Columns>
            <DataGridTemplateColumn MinWidth="100"
                                Header="Amount"
                                MaxWidth="{Binding MinWidth, RelativeSource={RelativeSource Self}}">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <mahApps:NumericUpDown Value="{Binding Amount, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="ValueChanged">
                                <i:InvokeCommandAction CommandParameter="{Binding}"
                                                       Command="{Binding DataContext.RefreshValuesCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </mahApps:NumericUpDown>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

编辑

FIRST:Cantidad(西班牙语)= Amount(英语)

【问题讨论】:

  • 究竟是什么不起作用?您是否有特定错误,即绑定 ItemsSource 的输出,或者该错误有效而内部绑定无效?考虑运行 Snoop 以查看哪些数据绑定失败。
  • 我要做的是:我有一个项目列表,我想使用 numericupdown 更改“金额”。当您更改“金额”时,将执行重新计算所有价格的命令。我需要将每个 numericupdown 绑定到项目的每个“数量”。转到编辑以查看更多代码和图像。错误:无法解析“System.Windows.Controls.DataGrid”类型的数据上下文中的属性“Amount”

标签: c# wpf


【解决方案1】:

如果 Amount 属性与 Articles 属性在同一类中定义,则应绑定到 DataGrid 的 DataContext (DataContext.Amount):

<mahApps:NumericUpDown Value="{Binding DataContext.Amount, UpdateSourceTrigger=PropertyChanged, 
    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="ValueChanged">
            <i:InvokeCommandAction CommandParameter="{Binding}"
                                   Command="{Binding DataContext.RefreshValuesCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</mahApps:NumericUpDown>

如果 Amount 属性是在数据对象(Article 或任何你称之为的)类中定义的,那么它就是:

<mahApps:NumericUpDown Value="{Binding Amount}">

编辑:您还应该将 Binding 的 UpdateSourceTrigger 设置为 PropertyChanged:

<mahApps:NumericUpDown Value="{Binding Amount, UpdateSourceTrigger=PropertyChanged}" />

【讨论】:

  • 感谢回复。我已经尝试了这两种方法,但没有奏效。金额是文章(类)属性。 Articles 是我在 ItemSource 中使用的 observableCollection。
  • 确保该属性是 public 并且是 double 类型。
  • 如果我使用一个简单的列 它工作得很好。当然有了这个,我只设法显示一个值而不是改变它。
  • 将Binding的UpdateSourceTrigger设置为PropertyChanged。我编辑了我的答案。
  • 删除 RelativeSource 部分。应该是 Value="{Binding Cantidad, UpdateSourceTrigger=PropertyChanged}"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-06
相关资源
最近更新 更多