UserControl получить обновленные данные из MainWindow - PullRequest
0 голосов
/ 05 апреля 2019

У меня есть 2 UserControl, где Usercontrol должен получить обновленные данные из MainWindow

<Window x:Class="BindingUserControl.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:BindingUserControl.Pages"
    xmlns:local1="clr-namespace:BindingUserControl"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">


<Window.Resources>
    <local1:CommonViewModel x:Key="ABC"/>
</Window.Resources>

<Grid>
    <local:UserControl1 DataContext="{Binding Source={StaticResource ABC}}" Margin="0,0,520.6,264"/>
    <TextBox  Width ="100" Height="100" Text="{Binding CommonProperity}"/>
    <Button Width="100" Height="100" RenderTransformOrigin="-1.85,1.404" Margin="139,208,554.6,112" Click="Button_Click"></Button>
</Grid>

UserControl

<UserControl x:Class="BindingUserControl.Pages.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"
         xmlns:Local="clr-namespace:BindingUserControl"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
    <Local:CommonViewModel x:Key="ABC"/>
</UserControl.Resources>
<Grid DataContext="{Binding Source={StaticResource ABC}}" >
    <TextBox Width="100" Height="100" Text="{Binding CommonProperity  ,Mode=TwoWay}" />
</Grid>

ViewModel

namespace BindingUserControl
{
    class CommonViewModel: INotifyPropertyChanged
    {

        private string _Localtextdata;
        public string CommonProperity
        {
            get { return _Localtextdata; }
            set
            {
                _Localtextdata = value;
                INotifyPropertyChanged("CommonProperity");
            }
        }

        private void INotifyPropertyChanged(string ProperityName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(ProperityName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

}

Нет обновленного текста в текстовом поле usercontrol, когда в текстовое поле Mainwindow поступает какая-либо запись.

Где моя ошибка?

1 Ответ

0 голосов
/ 05 апреля 2019

Вы должны создать только один экземпляр CommonViewModel и позволить UserControl наследовать DataContext из окна.Не устанавливайте явно DataContext UserControl или любых его дочерних элементов:

<Window x:Class="BindingUserControl.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:BindingUserControl.Pages"
    xmlns:local1="clr-namespace:BindingUserControl"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">

    <!-- Set the DataContext property -->
    <Window.DataContext>
        <local1:CommonViewModel x:Key="ABC"/>
    </Window.DataContext>

    <Grid>
        <local:UserControl1 Margin="0,0,520.6,264"/>
        <TextBox  Width ="100" Height="100" Text="{Binding CommonProperity}"/>
        <Button Width="100" Height="100" RenderTransformOrigin="-1.85,1.404" Margin="139,208,554.6,112" Click="Button_Click"></Button>
    </Grid>
</Window>

<UserControl x:Class="BindingUserControl.Pages.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"
         xmlns:Local="clr-namespace:BindingUserControl"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBox Width="100" Height="100" Text="{Binding CommonProperity  ,Mode=TwoWay}" />
    </Grid>
</UserControl>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...