MainViewModel与SubView的绑定属性

问题描述 投票:0回答:1

我试图弄清楚如何将MainWindowViewModel中的属性绑定到基于另一个View的ContentControl。RelativeSource绑定似乎不起作用,因为该视图位于另一个xaml文件中?我尝试了依赖属性,但我猜我不明白如何正确执行此操作。

我想在这里实现的是,当我在MainWindow的TextBox中键入内容时,所有3个视图(contentcontrols)也都将查看更新后的数据。它应该是一个演示,以说明在MVVM中ViewModel可以在不了解View的情况下进行更改,并且不同的View会对它做出反应。

Sallyly RelativeSource Binding and DependancyProperty对我不起作用,或者我错过了要点。

MainWindow.xaml

<Window x:Class="MVVMDemo.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"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <TextBlock Text="MVVM Demo" HorizontalAlignment="Center" FontSize="20" FontWeight="Bold"/>
        <TextBox Text="{Binding TestString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="200" Background="Black" Foreground="White" Margin="0 20 0 0"/>

        <Grid Margin="0 20 0 0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="300"/>
            </Grid.RowDefinitions>
            <ContentControl Grid.Column="0" Grid.Row="0" Content="{Binding ViewOne}"/>
            <ContentControl Grid.Column="1" Grid.Row="0" Content="{Binding ViewTwo}"/>
            <ContentControl Grid.Column="2" Grid.Row="0" Content="{Binding ViewThree}"/>

        </Grid>


    </StackPanel>
</Window>

MainWindowViewModel

    public class MainWindowViewModel : ViewModelBase
    {
        /// <summary>
        /// String to change the reaction of views
        /// </summary>
        private string _TestString;
        public string TestString
        {
            get { return _TestString; }
            set { _TestString = value; NotifyPropertyChanged(); }
        }

        private object _ViewOne { get; set; }
        public object ViewOne
        {
            get { return _ViewOne; }
            set { _ViewOne = value; NotifyPropertyChanged(); }
        }
        private object _ViewTwo { get; set; }
        public object ViewTwo
        {
            get { return _ViewTwo; }
            set { _ViewTwo = value; NotifyPropertyChanged(); }
        }
        private object _ViewThree { get; set; }
        public object ViewThree
        {
            get { return _ViewThree; }
            set { _ViewThree = value; NotifyPropertyChanged(); }
        }

        public MainWindowViewModel()
        {
            ViewOne = new ViewOne();
            ViewTwo = new ViewTwo();
            ViewThree = new ViewThree();
            TestString = "ABC";
        }
    }

ViewOneViewModel

<UserControl x:Class="MVVMDemo.Views.ViewOne"
             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:MVVMDemo.Views"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid Background="White">
        <StackPanel Orientation="Horizontal">

            <TextBlock Text="{Binding Path=TestString, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Foreground="Black"/>
            <TextBlock Text="{Binding TestStringProperty}" Foreground="Black"/>
        </StackPanel>


    </Grid>
</UserControl>

ViewOneViewModel

public class ViewOneViewModel : ViewModelBase
    {
        // this doesn't help
        public static readonly DependencyProperty TestStringProperty =
        DependencyProperty.Register("TestString", typeof(string), typeof(MainWindowViewModel), new PropertyMetadata(null));
    }
c# wpf data-binding
1个回答
0
投票

我相信您的问题就在这里:

    <TextBlock Text="{Binding Path=TestString, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Foreground="Black"/>

[Binding Path=TestString应该改为Binding Path=DataContext.TestString,因为RelativeSource现在正在查看窗口,而不是窗口的模型。

© www.soinside.com 2019 - 2024. All rights reserved.