C# 为第二个窗口设置绑定
我有两个WPF窗口,我想将上下文设置为ViewModel,但如果我写:C# 为第二个窗口设置绑定,c#,mvvm,datacontext,C#,Mvvm,Datacontext,我有两个WPF窗口,我想将上下文设置为ViewModel,但如果我写: this.DataContext = new myViewModel() 在我的第二个windows cs中,它不工作。这里是我的代码。我曾尝试在XAML中放置绑定并连接上下文,但当我尝试调试它时,我得到了错误代码,该断点将无法运行 BrowseDialog.xaml <Window x:Class="TextalkApi.BrowseDialog" xmlns="http://schemas.m
this.DataContext = new myViewModel()
在我的第二个windows cs中,它不工作。这里是我的代码。我曾尝试在XAML中放置绑定并连接上下文,但当我尝试调试它时,我得到了错误代码,该断点将无法运行
BrowseDialog.xaml
<Window x:Class="TextalkApi.BrowseDialog"
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:TextalkApi"
mc:Ignorable="d"
Title="BrowseDialog" Height="248.361" Width="427.459">
<Grid>
<Button Content="Browse" HorizontalAlignment="Left" Margin="267,11,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="FileDialog" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding webUrl}" VerticalAlignment="Top" Width="244"/>
<Button Content="Save" HorizontalAlignment="Left" Margin="267,166,0,0" VerticalAlignment="Top" Width="75" Command="{Binding SaveCommand}" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label Content="{Binding errorMessage}" HorizontalAlignment="Left" Margin="10,167,0,0" VerticalAlignment="Top" RenderTransformOrigin="-5.611,10.822" Width="207" Height="19"/>
</Grid>
</Window>
BrowseDialog.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using Data;
namespace TextalkApi
{
/// <summary>
/// Interaction logic for BrowseDialog.xaml
/// </summary>
public partial class BrowseDialog : Window
{
public BrowseDialog()
{
InitializeComponent();
this.DataContext = new BrowseViewModel();
}
}
}
您的XAML看起来是正确的,我高度怀疑是ViewModel导致它无法工作。这样的办法应该行得通 请注意,这在很大程度上取决于BaseViewModel的实现方式。如果可能的话,你能分享这一点,这样我可以更新我的答案是正确的吗?下面是如何在视图的DataContext ViewModel中实现属性
#region Properties
private string _webUrl;
public string WebUrl
{
get => _webUrl;
set
{
//This will change based on how you have implemented your BaseViewModel!
//The method name might be different, or have different parameters!
this.SetProperty(ref _webUrl, value, nameof(WebUrl));
//Call the save file path validation method...
SaveFilePath();
}
}
private string _errorMessage;
public string ErrorMessage
{
get => _errorMessage;
private set
{
//This will change based on how you have implemented your BaseViewModel!
//This method should call NotifyPropertyChange to notify the UI to update...
this.SetProperty(ref _errorMessage, value, nameof(ErrorMessage));
}
}
#endregion
在ViewModelBase中,可以添加一个通用的SetProperty方法,然后该方法可以为您处理引发property changed事件。大概是这样的:
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void SetProperty<T>(ref T storage, T value, string propertyName)
{
storage = value;
RaisePropertyChangedEvent(propertyName);
}
protected void RaisePropertyChangedEvent(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
由于ViewModel中的更改,您还需要更新XAML中的绑定
<Grid>
<Button Content="Browse" HorizontalAlignment="Left" Margin="267,11,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="FileDialog" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding WebUrl}" VerticalAlignment="Top" Width="244"/>
<Button Content="Save" HorizontalAlignment="Left" Margin="267,166,0,0" VerticalAlignment="Top" Width="75" Command="{Binding SaveCommand}" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label Content="{Binding ErrorMessage}" HorizontalAlignment="Left" Margin="10,155,0,0" VerticalAlignment="Top" RenderTransformOrigin="-5.611,10.822" Width="207" Height="46"/>
</Grid>
这里的问题是没有显示错误消息吗?你能澄清一下你的问题吗。我怀疑是的,在这种情况下,在errorMessage属性的setter中没有调用NotifyPropertyChanged或类似的,我怀疑您的ViewModelBase中有一个方法可以设置属性并自动调用实现该属性的方法。是的,这与问题很接近,但断点eaven似乎没有可能被击中。谢谢您共享您的ViewModelBase,我在他们的方法中看不到引发此事件的任何内容。我将用一个简单的例子更新我的anwser。你答案的顶部我应该在哪里添加它我进入viewmodel,但就像我添加属性一样?是的,抱歉顶部是viewmodel中的属性。在标签上的XAML中,try Content={Binding errorMessage,UpdateSourceTrigger=PropertyChanged}不起作用,它不会更新,但我仍然认为该命令是错误的否?
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void SetProperty<T>(ref T storage, T value, string propertyName)
{
storage = value;
RaisePropertyChangedEvent(propertyName);
}
protected void RaisePropertyChangedEvent(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
<Grid>
<Button Content="Browse" HorizontalAlignment="Left" Margin="267,11,0,0" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="FileDialog" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding WebUrl}" VerticalAlignment="Top" Width="244"/>
<Button Content="Save" HorizontalAlignment="Left" Margin="267,166,0,0" VerticalAlignment="Top" Width="75" Command="{Binding SaveCommand}" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Label Content="{Binding ErrorMessage}" HorizontalAlignment="Left" Margin="10,155,0,0" VerticalAlignment="Top" RenderTransformOrigin="-5.611,10.822" Width="207" Height="46"/>
</Grid>