views:

36

answers:

2

Hello,

I have a XAML-object (window-control) having his own-properties in the code-behind (in my case it has a property called 'FirstEditableDate' without any UI-binding).

I also have another XAML-object (user-control) with a property (also without UI) and I want to bind the other property to this property.

So, if the property of the (main)class is changing, the other property of the usercontrol is also changing.

How can I do this?

(see for examples my 'answer' below...)

+1  A: 

You can implement the INotifyPropertyChanged interface on the Main class and have the usercontrol handle the PropertyChange event.

Vivek
+1 Using INotifyPropertyChanged Or else, you can bind them like `this.SetBinding(FirstEditableDateProperty, new Binding(Source=this.userControlInstanceName, Path=new PropertyPath("PropertyInUserControl"), Mode=TwoWay));` Considering the property as DP.
Avatar
A: 

Here is some code:

MainWindow.cs:

namespace WpfApplicationProperties
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// This is a calculated property (in the real word a complex calculation...)
        /// </summary>
        public int MyCalculatedProperty
        {
            get
            {
                return (5 * 5);
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }
    }
}

and this is the usercontrol-code (UserControl1.cs):

namespace WpfApplicationProperties
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }


        private int _test2;

        /// <summary>
        /// This has to be the calculated result from the main-property
        /// </summary>
        public int MyProperty2
        {
            get
            {
                return _test2;  // return MyCalculatedProperty; ???
            }
        }


        private void what_Click(object sender, RoutedEventArgs e)
        {
            lab.Content = MyProperty2;
        }
    }
}

MainWindow.xaml:

<Window x:Class="WpfApplicationProperties.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:locals="clr-namespace:WpfApplicationProperties"
        Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen">
    <StackPanel Orientation="Vertical">
        <locals:UserControl1 Height="100" Width="300" x:Name="uc"/>
        <TextBox Text="{Binding MyCalculatedProperty, Mode=OneWay}" />
    </StackPanel>
</Window>

and

UserControl1.xaml:

<UserControl x:Class="WpfApplicationProperties.UserControl1"
             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="50" d:DesignWidth="200" Background="Silver">
    <StackPanel Orientation="Vertical">        
        <Button x:Name="what" Height="20" Content="What's my property?" Click="what_Click"/>
        <Label x:Name="lab" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
    </StackPanel>
</UserControl>

I want the property of the usercontrol (MyProperty2) bind to the property of the MainWindow (MyCalculatedProperty), so when I need the property in the usercontrol, the value of the calculated property from the mainwindow will returned.

I tried the INotifyPropertyChanged, but it did not working.

Please, can anyone help me???

Marcel