The following code runs if UpdateSourceTrigger is set to PropertyChanged but throws an exception upon initialization when UpdateSourceTrigger is set to LostFocus.
What is the problem with this implementation and how can it be corrected?
Exception
"'ComboBoxSample.ComboBoxBehavior' type must derive from FrameworkElement or FrameworkContentElement."
View
<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>
Behavior
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(); }
}
ViewModel
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; }
}
Your problem is that you're trying to use your
<local:ComboBoxBehavior />as if it was a block(a framework element). In the xaml, you can only write blocks that inherits from Framework Element(like paragraph, TextBlock, etc.). As I am not sure what you are trying to achieve, this is pretty much the best answer I can give with your level of detail.