-1

I'm trying to code a program where if you click on either a listbox item or a button in one usercontrol, it will update a textbox in another usercontrol. I can't seem to figure out how to get this working properly with dependencyproperties.

Listbox.xaml

<UserControl x:Class="TestDP3.ListBoxUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:local="clr-namespace:TestDP3">
<UserControl.DataContext>
    <local:MyViewModel/>
</UserControl.DataContext>
<Grid>
    <ListBox x:Name="lstbox" HorizontalAlignment="Left" Height="144" Margin="21,23,0,0" VerticalAlignment="Top" Width="149" SelectionChanged="Selector_OnSelectionChanged"/>

    <Button Content="Button" HorizontalAlignment="Left" Margin="105,231,0,0" VerticalAlignment="Top" Width="75" Command="{Binding TestCommand}"/>
    <TextBlock HorizontalAlignment="Left" Margin="52,272,0,0" TextWrapping="Wrap" Text="{Binding MyDp.Result}" VerticalAlignment="Top"/>

</Grid>

VnInfo.xaml

<UserControl x:Class="TestDP3.VnInfoUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             xmlns:local="clr-namespace:TestDP3">
    <UserControl.DataContext>
        <local:MyViewModel/>
    </UserControl.DataContext>
    <Grid>
        <TextBlock HorizontalAlignment="Left" Margin="10,102,0,0" TextWrapping="Wrap" Text="DependencyProperty is: " VerticalAlignment="Top"/>    
        <TextBlock HorizontalAlignment="Left" Margin="159,102,0,0" TextWrapping="Wrap" Text="{Binding MyDp.Result, Mode=TwoWay}" VerticalAlignment="Top"/>
    </Grid>
</UserControl>

MyViewModel.cs

public class MyViewModel : INotifyPropertyChanged
    {
        private MyDP _myDP;
        public event PropertyChangedEventHandler PropertyChanged;
        public MyViewModel()
        {
            _myDP = new MyDP();

            TestCommand = new MyCommand(SampleMethod);

        }

        public MyDP MyDp
        {
            get { return _myDP; }
            set
            {
                _myDP = value;
                RaisePropertyChanged("MyDP");              
            }
        }

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public ICommand TestCommand { get; set; }

        private int counter = 0;
        private int SampleMethod()
        {
            MyDp.Result = counter++;
            return MyDp.Result;  
        }
    }

MyDP.cs

public class MyDP : DependencyObject
    {
        public int Result
        {
            get { return (int)GetValue(ResultProperty); }
            set { SetValue(ResultProperty, value); }
        }
        public static readonly DependencyProperty ResultProperty =
            DependencyProperty.Register("Result", typeof(int), typeof(MyDP) ,new PropertyMetadata(0) 
            );        
    }

Here is a link to the visual studio project: https://dl.dropboxusercontent.com/u/22398345/TestDP.zip

  • I'm somewhat familiar with WPF and MVVM, but I've never toyed with setting the DataContext in the xaml. Is in each xaml file actually referencing the same object or is this call creating an all new object? – Tim Jan 05 '16 at 23:41
  • You are not doing right, it is not possible in this way, if you are using an mvvm framework try to use a messenger to send events between view models. Here is an example of mvvm light messenger: http://stackoverflow.com/questions/16993918/mvvm-light-messenger-sending-and-registering-objects – SomeCode.NET Jan 05 '16 at 23:59

1 Answers1

0

Both views must access the same object (ViewModel). I suspect your calls to

<local:MyViewModel/>

in each of the Xaml files are creating separate objects. You'll need to create a single MyViewModel object in your code-behind somewhere:

MyViewModel vm = new MyViewModel();

and pass a reference of it to each of the views, either through the constructor:

ListBoxUserControl(MyViewModel vm) { ... }

or to a property:

ListBoxUserControl.MyViewModel = vm;

Then you can then set your DataContext in the code-behind:

this.DataContext = vm;
Tim
  • 857
  • 6
  • 13