none
IsSynchronizedWithCurrentItem

    Question

  • Hi,

    I have a usercontrol with a combobox.  The usercontrol is placed in a window twice like this:

        <StackPanel>
            <local:UserControl1 x:Name="UC1" Width="Auto" Height="Auto"></local:UserControl1>
            <local:UserControl1 x:Name="UC2" Width="Auto" Height="Auto"></local:UserControl1>
        </StackPanel>

    The usercontrol XAML is like this.  Note that IsSynchronizedWithCurrentItem="True"

        <StackPanel>
            <ComboBox x:Name="CBX" ItemsSource="{Binding Items}" 
                      DisplayMemberPath="Name" 
                      IsSynchronizedWithCurrentItem="True"
                      SelectedItem="SelectedPOCO"></ComboBox>
        </StackPanel>

    If I set the usercontrols' DataContext like below, the comboboxes stay in sync, which is to be expected.

            public Window1()
            {
                InitializeComponent();
                this.DataContext = new VM();
            }

    When I set the DataContext of the usercontrols individually like below, I'd expect that they will no longer stay in sync as their DataContexts now are different instances of the view model.  But they still do.  Why is that?

            public Window1()
            {
                InitializeComponent();
                this.UC1.DataContext = new VM();
                this.UC2.DataContext = new VM();
            }

    I do want them to unsync from each other.  The way I did it is set the IsSync property of one of them to false in code-behind like below.  Is this the best way to do it?  Can it be done outside of code-behind?

            public Window1()
            {
                InitializeComponent();
                UC1.CBX.IsSynchronizedWithCurrentItem = false;
                this.DataContext = new VM();
            }
    Thursday, April 08, 2010 7:25 PM

Answers

  • Hi Stratoblue,

    So that the two combo box items source are the same collection, that may be the reason that the combo box will synchronize with current item simultaneously.

    If you don’t want to set the IsSynchronizedWithCurrentItem property in the code-behind, you can encapsulate that property as a UserControl property, so that you can set it in XAML of the user control tag.
    Something like the follows:
    XAML

    <UserControl x:Class="WpfApplication1.UserControl1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Height="300" Width="300">

        <StackPanel>

            <ComboBox Name="myComboBox"></ComboBox>

        </StackPanel>

    </UserControl>
    Code-behind

    public bool? IsSynchronizedWithCurrentItem

    {

        get

        {

            return this.myComboBox.IsSynchronizedWithCurrentItem;

        }

        set

        {

            this.myComboBox.IsSynchronizedWithCurrentItem = value;

        }

    }

     

    Hope this helps.

    Best regards,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Thursday, April 15, 2010 2:22 AM
    Tuesday, April 13, 2010 4:46 AM

All replies

  • Do the two instances of VM share the same collection as their Items property?
    Thursday, April 08, 2010 9:55 PM
  • Hi Sam,

    Yes they do.

     

    Friday, April 09, 2010 12:12 PM
  • Hi Stratoblue,

    So that the two combo box items source are the same collection, that may be the reason that the combo box will synchronize with current item simultaneously.

    If you don’t want to set the IsSynchronizedWithCurrentItem property in the code-behind, you can encapsulate that property as a UserControl property, so that you can set it in XAML of the user control tag.
    Something like the follows:
    XAML

    <UserControl x:Class="WpfApplication1.UserControl1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Height="300" Width="300">

        <StackPanel>

            <ComboBox Name="myComboBox"></ComboBox>

        </StackPanel>

    </UserControl>
    Code-behind

    public bool? IsSynchronizedWithCurrentItem

    {

        get

        {

            return this.myComboBox.IsSynchronizedWithCurrentItem;

        }

        set

        {

            this.myComboBox.IsSynchronizedWithCurrentItem = value;

        }

    }

     

    Hope this helps.

    Best regards,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Thursday, April 15, 2010 2:22 AM
    Tuesday, April 13, 2010 4:46 AM
  • Thanks, Linda.  I will stay with the code-behind I originally wrote and revisit later to make sure the two VM instances do not share a common collection. 
    Tuesday, April 13, 2010 1:45 PM