![](/img/trans.png)
[英]How do I override the ViewModel DataContext so I can bind to objects in the View (Mvvm-Light)?
[英]How can i bind a view(having datacontext of own viewmodel) with another viewmodel's property
有兩個視圖和兩個相應的視圖模型。 現在我在 dasboardviewmodel 中有一個狀態屬性。 我必須在主視圖中綁定該屬性。 視圖和視圖模型在數據模板中鏈接。 我試圖在家里調用儀表板 ViewModel 數據上下文,但它不起作用。 請任何人都可以幫助我或提供一個簡單的例子,謝謝。
<UserControl x:Class="Demo.View.Home"
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:Demo.View"
xmlns:viewmodel="cnamespace:Demo.ViewModel"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="BlueViolet">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox Content="second" IsChecked="{Binding status}" Margin="200,100,0,0"/>
</Grid>
</UserControl>
儀表板視圖...
<UserControl x:Class="Demo.View.Dashboard"
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:Demo.View"
xmlns:vm="clr-namespace:Demo.ViewModel"
xmlns:dt="clr-namespace:DefectTracking;assembly=DefectTrackingControl"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
>
<Grid Background="Red">
<StackPanel>
<CheckBox Content="one" IsChecked="{Binding status}" Margin="200,100,10,10"/>
</StackPanel>
</Grid>
</UserControl>
HomeViewmodel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System. Linq;
using System. Text;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace Demo.ViewModel
{
public class HomeViewModel : INotifyPropertyChanged
{
private bool _check;
public bool Check
{
get { return _check; }
set { _check = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
DashboardViewModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using DefectTracking;
using System.Runtime.CompilerServices;
using System.Windows;
namespace Demo.ViewModel
{
public class DashboardViewModel : DependencyObject, INotifyPropertyChanged
{
public DispatcherTimer DispatchTimer = new DispatcherTimer();
public DashboardViewModel()
{
DispatchTimer.Interval = TimeSpan.FromMilliseconds(1000);
DispatchTimer.Tick += dispatchTimer_Tick;
DispatchTimer.Start();
}
private bool _status;
public bool status
{
get
{
return _status;
}
set
{
_status = value;
OnPropertyChanged(nameof(status));
}
}
private void dispatchTimer_Tick(object sender, EventArgs e)
{
status = !status;
}
//public event PropertyChangedEventHandler PropertyChanged;
//public void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
// => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
您可以定義一個 singleton class 作為端口, DashboardViewModel
將在狀態屬性更新時更新它。
public class DashboardPort : INotifyPropertyChanged
{
private DashboardPort()
{
}
public static DashboardPort Instance = new DashboardPort();
private bool _status;
public bool Status
{
set
{
_status = value;
OnPropertyChanged(nameof(Status));
}
get => _status;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
在兩個視圖模型中,您將定義端口的一個實例。
public DashboardPort DashboardPort => DashboardPort.Instance;
在DashboardViewModel
中,您將在狀態屬性更新時更新端口
private bool _status;
public bool status
{
get
{
return _status;
}
set
{
_status = value;
OnPropertyChanged(nameof(status));
DashboardPort.Status = value;
}
}
最后, Dashboard.xaml
中的 CheckBox 將從 Port 實例中讀取其 state。
<CheckBox Content="second"
IsChecked="{Binding DashboardPort.Status}"
Margin="200,100,0,0"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.