使数据绑定与Datagrid一起工作的问题

时间:2013-12-09 19:00:32

标签: wpf xaml data-binding mvvm mvvm-light

我使用的是MVVM Light框架,但我遇到的问题是我已经绑定到

的属性

我有一个对象BuyoutRule

public class BuyoutRule : ViewModelBase
{
   /*
   More private fields and properties
   */
    public int MaximumPrice
    {
        get { return _maximumPrice; }
        set
        {
            _maximumPrice = value;
            RaisePropertyChanged(() => MaximumPrice);
        }
    }

    public bool Enabled
    {
        get { return _enabled; }
        set
        {
            _enabled = value;
            RaisePropertyChanged(() => Enabled);
        }
    }

这些对象位于此类

中的ObservableCollection中
public class BuyoutCheapModuleViewModel : ViewModelBase
{

private ObservableCollection<BuyoutRule> _buyoutRules;
public ObservableCollection<BuyoutRule> BuyoutRules
    {
        get { return _buyoutRules; }
        set
        {
            _buyoutRules = value; 
            RaisePropertyChanged(() => BuyoutRules);
        }
    }
public BuyoutCheapModuleViewModel(IBuyoutRuleQuery buyoutRuleQuery)
      {
            BuyoutRules = new ObservableCollection<BuyoutRule>();
      }
}

然后我将具有前一个类的View作为Datacontext

<Grid>
    <ScrollViewer>
        <StackPanel>
            <DataGrid ItemsSource="{Binding BuyoutRules, Mode=TwoWay}"
                      SelectedItem="{Binding SelectedBuyoutRule, Mode=TwoWay}"
                      AutoGenerateColumns="False" CanUserAddRows="False">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Last Name" IsReadOnly="True">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Player.LastName}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Rating" IsReadOnly="True">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Player.Rating}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTemplateColumn Header="Last Name" IsReadOnly="True">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Player.Position}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Header="Mininum Buyout" IsReadOnly="False" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <xctk:IntegerUpDown Increment="1000"
                                                    Value="{Binding MinimumBuyoutPrice, Mode=TwoWay}" 
                                                    Minimum="0"
                                                    FormatString="N0"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Header="Maximum Buyout">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <xctk:IntegerUpDown Increment="1000"
                                                    Value="{Binding MaximumPrice, Mode=TwoWay}" 
                                                    Minimum="0"
                                                    FormatString="N0"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Header="Enabled">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding Enabled, Mode=TwoWay}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
            <StackPanel Orientation="Horizontal">
                <Button Content="Remove" Command="{Binding RemoveSelectedCommand}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding QuickbuyEnabled}">Enable quickbuy</CheckBox>
            </StackPanel>
        </StackPanel>
    </ScrollViewer>
</Grid>

但是如果我在SetP中设置断点,无论是MaximumPrice还是Enabled,并且更改我的UI中的值没有任何反应,我无法触发设置器,它们被触发的唯一时间是创建视图时的初始值加载的数据填写

1 个答案:

答案 0 :(得分:1)

数据网格单元格的默认UpdateSourceTrigger是UpdateSourceTrigger=LostFocus,但您只需使用CellTemplate,这意味着它永远不会被击中。将其更改为PropertyChanged将允许您进行TwoWay绑定。