与UpdateSourceTrigger绑定设置为LostFocus的异常

时间:2015-08-25 15:11:26

标签: wpf mvvm binding behavior updatesourcetrigger

如果UpdateSourceTrigger设置为PropertyChanged,则会运行以下代码,但在UpdateSourceTrigger设置为LostFocus时初始化时会抛出异常。

此实施有什么问题?如何纠正?

异常

"'ComboBoxSample.ComboBoxBehavior' type must derive from FrameworkElement or FrameworkContentElement."

查看

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>

<Grid>
    <ComboBox ItemsSource="{Binding Path=Apples}"
              DisplayMemberPath="Cultivar">
        <i:Interaction.Behaviors>
            <local:ComboBoxBehavior 
                SelectedValue="{Binding Path=SelectedId, 
                                        Mode=TwoWay,
                                        UpdateSourceTrigger=LostFocus}"/>
        </i:Interaction.Behaviors>
    </ComboBox>
</Grid>

行为

public class ComboBoxBehavior : Behavior<ComboBox>
{
    public static readonly DependencyProperty SelectedValueProperty
        = DependencyProperty.Register("SelectedValue",
                                      typeof(object),
                                      typeof(ComboBoxBehavior),
                                      new FrameworkPropertyMetadata(null, (target, args) => { }));

    public object SelectedValue
    {
        get { return GetValue(SelectedValueProperty); }
        set { SetValue(SelectedValueProperty, value); }
    }

    protected override void OnAttached() { base.OnAttached(); }

    protected override void OnDetaching() { base.OnDetaching(); }
}

视图模型

public class ViewModel
{
    public ObservableCollection<Apple> Apples { get; set; }

    public int SelectedId { get; set; }

    public ViewModel()
    {
        Apples = new ObservableCollection<Apple>
        {
            new Apple()
            {
                Id = 0,
                Cultivar = "Alice",
                Weight = 0.250
            },
            new Apple()
            {
                Id = 1,
                Cultivar = "Golden",
                Weight = 0.3
            },
            new Apple()
            {
                Id = 2,
                Cultivar = "Granny Smith",
                Weight = 0.275
            }
        };
    }
}

public class Apple
{
    public int Id { get; set; }

    public string Cultivar { get; set; }

    public double Weight { get; set; }
}

2 个答案:

答案 0 :(得分:0)

您的问题是您尝试使用<local:ComboBoxBehavior />,就好像它是一个块(框架元素)。在xaml中,您只能编写从Framework Element继承的块(如段落,TextBlock等)。由于我不确定你想要达到的目标,这几乎是我能给出的最佳答案。

答案 1 :(得分:0)

我自己也很陌生,所以不确定它会有多大帮助。首先,您是否尝试一起更新SelectedItem和SelectedValue?如果是这样,可能不需要行为(警告,未经测试的代码!):

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>

<Grid>
    <ComboBox ItemsSource="{Binding Path=Apples}"
          DisplayMemberPath="Cultivar" SelectedValue="{Binding Path=SelectedId, 
                                    Mode=TwoWay,
                                    UpdateSourceTrigger=LostFocus}" >
    </ComboBox>
</Grid>

或类似的东西将组合框的SelectedValue直接绑定到视图模型中的SelectedId。然后,如果在更改SelectedValue时需要在行为中执行某些操作,请将其创建为与SelectedId更新分开的自己的行为。这有帮助吗?