Usercontrol databinding property changed MVVM

I am working with WPF and using data binding.

I would like to make a UserControl which has a property that could be used for data binding.

Also, I want to update some other property in the UserControl if the property changed.

For example,

public class MyControl : UserControl
{
....
....
....
....

        public ViewStyles CurrentView
        {
            get { return (ViewStyles)GetValue(CurrentViewProperty); }
            set
            {
                SetValue(CurrentViewProperty, value);
                UpdateView();
            }
        }
        public static readonly DependencyProperty CurrentViewProperty = DependencyProperty.Register("CurrentView", typeof(ViewStyles), typeof(ComboView));

....
.....
.....
.....
}

Problems comes:

A ViewModel is used and in which, there is a property ViewStyle which binded to the CurrentView in the above.

Another control combobox is also data-binded with ViewStyle in the ViewModel.

Actually, I want to use a combobox to choose the different view of my control. How to make it possible in MVVM?

I tried the above method. However, the UI (the different ViewStyles of MyControl) didn't change. It only change when I click on it using the mouse.

Thank you.

XAML: (MyControl)

<Views:MyControl Grid.Column="1" Grid.Row="1" Height="505" HorizontalAlignment="Left" Margin="2,0,0,0" Name="comboView1" VerticalAlignment="Top" Width="983" 
                 ViewStyle="{Binding Path=CurrentView}" BorderThickness="5" BorderBrush="Black" ItemsSource="{Binding Path=Images}" 
                 SelectedIndex="{Binding Path=CurrentIndex}" Foreground="White" 

</Views:MyControl>

XAML: (ComboBox)

<ComboBox Margin="0,3,1,0" Width="178" HorizontalAlignment="Right" Name="ViewDDBox" FontSize="13" Foreground="#FFF6F3F3" Background="#FF444444"
          BorderThickness="2" Height="23" VerticalAlignment="Top" Grid.Column="1" 
          ItemsSource="{Binding Path=ViewTypes}" IsEnabled="True" SelectedValue="{Binding Path=CurrentView, Mode=TwoWay}">
         </ComboBox>

It is supposed that the view (some UI effect) will be changed of MyControl after choosing in the Combobox. But now, it only change when I click on MyControl using mouse.

Answers


The UpdateView() in your CurrentView property setter raises a HUGE red flag! You should never have any content other than SetValue in a dependency property setter, as certain aspects of xaml call the SetValue directly instead of going through the property. Always use the coerce property callback (if you want to validate the data before it's set) or the property changed callback (if you want to act after the property is changed, as I show in the example below).

You should do this instead:

public static DependencyProperty CurrentViewProperty = 
    DependencyProperty.Register("CurrentView", typeof(ViewStyles), typeof(ComboView),
    new FrameworkPropertyMetadata(CurrentViewPropertyChanged));

private static void CurrentViewPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    MyControl mc = (MyControl)d;
    mc.UpdateView();
}

Need Your Help

c++: Use type as key for a map/dictionay?

c# c++ templates types

I have the following members in one of my C# classes:

Comparing Arrays of Objects in JavaScript

javascript arrays comparison

I want to compare 2 arrays of objects in JavaScript code. The objects have 8 total properties, but each object will not have a value for each, and the arrays are never going to be any larger than 8

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.