c# - WPF 数据与 ResourceDictionary MVVM 绑定(bind)

标签 c# wpf mvvm binding

我试图在 ResourceDictionary 中将 View 与 ViewModel 绑定(bind),但它不起作用。

该应用程序是一个非常简单的窗口,带有 2 个文本框。当我向 textbox1 键入文本时,textbox2 必须自动获得相同的文本。当然,View 中的文本框必须绑定(bind)到 ViewModel 中的属性。

我是 WPF 的新手,我开始绑定(bind) View 和 View 模型的方式是在 View 的代码隐藏中:

DataContext = new MyViewModel();

现在我正在努力实现更清晰的分离。我的代码是

应用程序.xaml:

<Application x:Class="NavigationCleanBinding.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="/Views/MainWindowView.xaml">
    <Application.Resources>
        <ResourceDictionary Source="MainResourceDictionary.xaml" />
    </Application.Resources>
</Application>

MainResourceDictionary.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xamlpresentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Views="clr-namespace:NavigationCleanBinding.Views"
    xmlns:ViewModels="clr-namespace:NavigationCleanBinding.ViewModels">

    <DataTemplate DataType="{x:Type ViewModels:MainWindowViewModel}">
        <Views:MainWindowView />
    </DataTemplate>

</ResourceDictionary>

MainWindowView.xaml:

<Window x:Class="NavigationCleanBinding.Views.MainWindowView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<Grid>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="61,14,0,0" 
             Name="textBox1" VerticalAlignment="Top" Width="120" 
             Text="{Binding TestData, Mode=TwoWay,
             UpdateSourceTrigger=PropertyChanged}"/>
    <Label Content="Test:" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0"
      Name="label1" VerticalAlignment="Top" Width="43" />
    <Label Content="Result:" Height="28" HorizontalAlignment="Left" Margin="10,46,0,0"
      Name="label2" VerticalAlignment="Top" />

    <TextBox Height="23" HorizontalAlignment="Left" Margin="61,48,0,0"
             Name="textBox2" VerticalAlignment="Top" Width="120" 

             Text="{Binding TestData, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>

主窗口 View 模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NavigationCleanBinding.ViewModels
{
    class MainWindowViewModel
    {
        private String _testData;
        public String TestData
        {
            get { return _testData; }
            set { _testData = value; }
        }

        private MainWindowViewModel()
        {
            _testData = null;
        }
    }
}

更新:

我将属性 TestData 更改为:

public String TestData
    {
        get { return _testData; }
        set
        { 
            _testData = value;
            OnPropertyChanged("TestData");

        }
    }

并像这样实现 INotifyPropertyChanged:

public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

最佳答案

所以 user1064519 是在正确的轨道上:

  • View 需要是 UserControl,而不是 Window,因为它托管在 MainWindow 中
  • ViewModel 需要加载到 MainWindow 中,这是触发 DataTemplate 被发现和加载的原因。

    <Window x:Class="WpfTemplateBootstrap.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTemplateBootstrap"
        Title="MainWindow" Height="350" Width="525">
        <ContentControl>
            <ContentControl.Content>
                <local:MainWindowViewModel />
            </ContentControl.Content>
        </ContentControl>
    

之后,您应该可以正常运行了。 我在这里发布了一个深入的例子:wpf bootstrapping datatemplates--the chicken and the egg

关于c# - WPF 数据与 ResourceDictionary MVVM 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14102310/

相关文章:

c# - BitConverter.ToInt32 是如何工作的?

c# - HttpClient 抛出 System.ArgumentException : 'windows-1251' is not a supported encoding name

java - Dagger MVVM - ViewModel 注入(inject)为空

c# - 从 UWP 中的 ViewModel B 更新 ViewModel A 的属性

android - ViewModelProvider.NewInstanceFactory - 接收相同的实例

c# - .Net 中的自定义安装程序显示安装程序后面的表单

c# - 我可以覆盖 UserControl 中的 css 类吗?

c# - 我如何关闭大写锁定键

.net - 云端桌面WPF应用程序部署

c# - DataGrid 的 CellEditingTemplate 和编辑模式下的焦点