NumericUpDown值绑定

时间:2017-01-09 15:00:18

标签: c# wpf

我有问题。如何将 ItemsSource 属性绑定到NumericUpDown?这样它就不起作用了。 THX!

<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(西班牙语)=金额(英语)

enter image description here enter image description here

1 个答案:

答案 0 :(得分:2)

如果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属性是在数据对象(文章或任何你称之为)中定义的,那么它只是:

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

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

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