0

I have three windows in whose i need a single value synchronized. I need to update the selected item across thre windows. Two of them containing a combobox and the main window containing a label. Thus, i need the two comboboxes to be identical and the label do update reflecting the selected item in the comboboxes.

Curently I get the two combos syncronized but my question is how to sync the label. I´ve tried using Properties, using DependencyProperties, Implementing INotifyPropertyChanged but no luck with the label

i have this example code failing to synchronize the label:

MainWindow.cs:

public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public CollectionView Items { get; set; }
        private string _selectedItem;
        public event PropertyChangedEventHandler PropertyChanged;
        public string SelectedItem
        {
            get { return (string)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }

        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem",
        typeof(string), typeof(MainWindow), new UIPropertyMetadata(""));

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public MainWindow()
        {
            DataContext = this;
            InitializeComponent();
            Left = 100; Width = 350; Height = 200; Top = 10;
            Business business = new Business();
            Items = new CollectionView(business.GetItemsFromWebService());
            Wnd1 wnd1 = new Wnd1();
            wnd1.Left = 100; wnd1.Width = 350; wnd1.Height = 200; wnd1.Top = 210;
            wnd1.Show();

            Wnd2 wnd2 = new Wnd2();
            wnd2.Left = 100; wnd2.Width = 350; wnd2.Height = 200; wnd2.Top = 410;
            wnd2.Show();

            wnd1.cbItems.ItemsSource = Items;
            wnd1.cbItems.SelectedValue = SelectedItem;
            wnd2.cbItems.ItemsSource = Items;
            wnd2.cbItems.SelectedValue = SelectedItem;

            Binding labelBinding = new Binding();
            labelBinding.Mode = BindingMode.TwoWay;
            labelBinding.Source = SelectedItem;
            labelBinding.Path = new PropertyPath("SelectedItem");
            lbSelected.SetBinding(Label.ContentProperty, labelBinding);

            Binding cmbBindingW1 = new Binding();
            cmbBindingW1.Mode = BindingMode.TwoWay;
            cmbBindingW1.Source = SelectedItem;
            cmbBindingW1.Path = new PropertyPath("SelectedItem");
            wnd1.cbItems.SetBinding(ComboBox.SelectedItemProperty, cmbBindingW1);
            wnd1.cbItems.SelectionChanged += CbItems_SelectionChanged;

            Binding cmbBindingW2 = new Binding();
            cmbBindingW2.Mode = BindingMode.TwoWay;
            cmbBindingW2.Source = SelectedItem;
            cmbBindingW2.Path = new PropertyPath("SelectedItem");
            wnd1.cbItems.SetBinding(ComboBox.SelectedItemProperty, cmbBindingW2);
        }

        private void CbItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int a = 0;
            SelectedItem = ((ComboBox)sender).SelectedItem.ToString();
        }
    }

MainWindow.xaml:

<Window x:Class="DropDownSync.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DropDownSync"
        mc:Ignorable="d"
        Title="MainWindow" Height="183.333" Width="376.667"
        Name="wMain">
    <Grid>
        <Label Name="lbSelected" HorizontalAlignment="Left" Margin="10,71,0,0" VerticalAlignment="Top" Width="80"
               Visibility="Visible"/>
    </Grid>
</Window>

Business.cs:

public class Business
    {
        public List<string> GetItemsFromWebService()
        {
            List<string> result = new List<string>();
            result.Add("item1");                
            result.Add("item2");
            result.Add("item3");
            return result;
        }
    }

Wnd1.xaml:

<Window x:Class="DropDownSync.Wnd1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DropDownSync"
        mc:Ignorable="d"
        Title="Wnd1" Height="211.667" Width="518.333"
        Name="w1">
    <Grid>
        <ComboBox Name="cbItems" HorizontalAlignment="Left" Margin="90,14,0,0" VerticalAlignment="Top" Width="120"/>
        <Label Content="Current item" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="80"/>
    </Grid>
</Window>

Wnd2.xaml:

<Window x:Class="DropDownSync.Wnd2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DropDownSync"
        mc:Ignorable="d"
        Title="Wnd2" Height="190" Width="453.333"
        Name="w2">
    <Grid>
        <ComboBox Name="cbItems" HorizontalAlignment="Left" Margin="90,14,0,0" VerticalAlignment="Top" Width="120"/>
        <Label Content="Current item" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="80"/>
    </Grid>
</Window>
RubenDFC
  • 35
  • 7

1 Answers1

1

new CollectionView() causes Visual Studio 2017 to output this in debug output:

System.Windows.Data Warning: 53 : Using CollectionView directly is not fully supported. The basic features work, although with some inefficiencies, but advanced features may encounter known bugs. Consider using a derived class to avoid these problems.

Instead, I'd change it to type ICollectionView and then set Items to CollectionViewSource.GetDefaultView(business.GetItemsFromWebService());

Also, you set wnd1.cbItems.Binding(ComboBox.SelectedItemProperty, xxx) twice. I assume the 2nd one was supposed to be wnd2.

And the SelectionChanged event handler isn't needed, if bindings are correct.

Here is an updated MainWindow.cs with the changes:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public ICollectionView Items { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    public string SelectedItem
    {
        get { return (string)GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem",
    typeof(string), typeof(MainWindow), new UIPropertyMetadata(""));

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
        Left = 100; Width = 350; Height = 200; Top = 10;
        Business business = new Business();
        Items = CollectionViewSource.GetDefaultView(business.GetItemsFromWebService());

        Wnd1 wnd1 = new Wnd1();
        wnd1.Left = 100; wnd1.Width = 350; wnd1.Height = 200; wnd1.Top = 210;
        wnd1.Show();

        Wnd2 wnd2 = new Wnd2();
        wnd2.Left = 100; wnd2.Width = 350; wnd2.Height = 200; wnd2.Top = 410;
        wnd2.Show();

        wnd1.cbItems.ItemsSource = Items;
        wnd2.cbItems.ItemsSource = Items;

        Binding labelBinding = new Binding();
        labelBinding.Mode = BindingMode.TwoWay;
        labelBinding.Source = this;
        labelBinding.Path = new PropertyPath("SelectedItem");

        // no need to make an identical bindings, just use the same one again
        lbSelected.SetBinding(Label.ContentProperty, labelBinding);
        wnd1.cbItems.SetBinding(ComboBox.SelectedItemProperty, labelBinding);
        wnd2.cbItems.SetBinding(ComboBox.SelectedItemProperty, labelBinding);
    }
}
J.H.
  • 4,232
  • 1
  • 18
  • 16
  • Thank you very much. It´s working in this way. But, I still wonder for the reason for the binding was not working. It was due to ICollectionView or due to the xxx.Source not properly defined. – RubenDFC Mar 19 '19 at 22:09