Wpf UpdateSourceTrigger设置为LostFocus的绑定异常

Wpf UpdateSourceTrigger设置为LostFocus的绑定异常,wpf,mvvm,binding,behavior,updatesourcetrigger,Wpf,Mvvm,Binding,Behavior,Updatesourcetrigger,如果UpdateSourceTrigger设置为PropertyChanged,则运行以下代码,但当UpdateSourceTrigger设置为LostFocus时,在初始化时引发异常 此实现存在什么问题?如何纠正 例外情况 "'ComboBoxSample.ComboBoxBehavior' type must derive from FrameworkElement or FrameworkContentElement." 查看 <Window.DataContext>

如果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(); }
}
公共类ComboBoxBehavior:行为
{
公共静态只读依赖属性SelectedValueProperty
=DependencyProperty.Register(“SelectedValue”,
类型(对象),
类型(ComboBoxBehavior),
新的FrameworkPropertyMetadata(null,(target,args)=>{});
公共对象SelectedValue
{
获取{返回GetValue(SelectedValueProperty);}
set{SetValue(SelectedValueProperty,value);}
}
受保护的覆盖无效OnAttached(){base.OnAttached();}
受保护的重写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; }
}
公共类视图模型
{
公共可观测集合{get;set;}
public int SelectedId{get;set;}
公共视图模型()
{
苹果=新的可观察收集
{
新苹果
{
Id=0,
栽培品种=“爱丽丝”,
重量=0.250
},
新苹果
{
Id=1,
品种=“金色”,
重量=0.3
},
新苹果
{
Id=2,
栽培品种=“史密斯奶奶”,
重量=0.275
}
};
}
}
公营苹果
{
公共int Id{get;set;}
公共字符串类型{get;set;}
公共双权重{get;set;}
}

您的问题是,您试图将
当作块(框架元素)来使用。在xaml中,您只能编写从框架元素继承的块(如段落、文本块等)。由于我不确定您要实现什么目标,这几乎是我就您的详细程度所能给出的最佳答案。

您的问题是,您试图将您的
当作一个块(框架元素)来使用。在xaml中,您只能编写从框架元素继承的块(如段落、文本块等)。由于我不确定您想要实现什么目标,这几乎是我根据您的详细程度所能给出的最佳答案。

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



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

我自己对这方面还不太熟悉,所以不确定这会有多大帮助。首先,您是否要同时更新SelectedItem和SelectedValue?如果是这样,可能不需要行为(警告,未测试的代码!):



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

谢谢你的评论。我正在处理的实际场景更复杂,这种复杂性在这里没有附加值。问题归结为我上面描述的情况,只要绑定到依赖项属性的UpdateSourceTrigger未设置为LostFocus,一切都可以正常工作。当设置为LostFocus时,相应的视图在初始化时抛出异常。感谢您的评论。我正在处理的实际场景更复杂,这种复杂性在这里没有附加值。问题归结为我上面描述的情况,只要绑定到依赖项属性的UpdateSourceTrigger未设置为LostFocus,一切都可以正常工作。当设置为LostFocus时,相应的视图在初始化时会引发异常。我的目标是找出为什么在所描述的情况下,将依赖项属性的绑定上的UpdateSourceTrigger设置为LostFocus会引发异常,而将触发器设置为PropertyChanged则可以正常工作。请注意,上面的代码只是演示了这种行为。我的目标是找出为什么在所描述的情况下,将依赖项属性的绑定上的UpdateSourceTrigger设置为LostFocus会引发异常,而将触发器设置为PropertyChanged则可以正常工作。请注意,上面的代码只是演示了这种行为。
<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>

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