苏州哪家做网站好些,推广之家app,辽宁建设工程信息网查询官网,关键词竞价排名名词解释为了确保的新 ViewModel 不会影响现有绑定到 MainViewModel 的其他属性#xff0c;可以使用 MonitorPage 作为 UserControl 的局部 DataContext#xff0c;而不覆盖整个 UserControl 的 DataContext。可以通过在 XAML 中的某个局部范围内#xff08;如包含时间显示的 TextBl…为了确保的新 ViewModel 不会影响现有绑定到 MainViewModel 的其他属性可以使用 MonitorPage 作为 UserControl 的局部 DataContext而不覆盖整个 UserControl 的 DataContext。可以通过在 XAML 中的某个局部范围内如包含时间显示的 TextBlock设置局部 DataContext 来实现。
具体步骤如下
1. **不要在整个 UserControl 设置 DataContext**而是在需要显示时间的 TextBlock 上设置局部 DataContext。 2. **保留原有的 DataContext**这样其他绑定会继续使用 MainViewModel。
### 修改后的 XAML
在包含时间显示的 TextBlock 上设置局部 DataContext如下所示
xml UserControl x:ClassZhaoxi.DigitaPlatform.Views.Pages.MonitorPage xmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:dhttp://schemas.microsoft.com/expression/blend/2008 xmlns:localclr-namespace:Zhaoxi.DigitaPlatform.Views.Pages xmlns:lvcclr-namespace:LiveCharts.Wpf;assemblyLiveCharts.Wpf xmlns:zxcclr-namespace:Zhaoxi.DigitaPlatform.Components;assemblyZhaoxi.DigitaPlatform.Components xmlns:cclr-namespace:Zhaoxi.DigitaPlatform.Common.Converter;assemblyZhaoxi.DigitaPlatform.Common xmlns:sysclr-namespace:System;assemblymscorlib mc:Ignorabled d:DesignHeight750 d:DesignWidth1300 UserControl.Resources !-- Your existing resources -- /UserControl.Resources !-- Your existing layout -- Grid Margin80,10,10,10 Grid.ColumnDefinitions ColumnDefinition Width250/ ColumnDefinition/ ColumnDefinition Width250/ /Grid.ColumnDefinitions !-- 第一列 -- Grid Grid.RowDefinitions RowDefinition Height90/ RowDefinition/ /Grid.RowDefinitions !-- 第一块 -- Border CornerRadius6 Border.Background LinearGradientBrush StartPoint0,0 EndPoint0,1 GradientStop Color#1116a1ff Offset0/ GradientStop ColorTransparent Offset1/ /LinearGradientBrush /Border.Background /Border Grid VerticalAlignmentTop Margin0,10 !-- Your existing layout -- /Grid !-- 在这里设置局部 DataContext -- TextBlock VerticalAlignmentBottom HorizontalAlignmentCenter Margin0,0,0,15 Foreground#555 Nametxt_time TextBlock.DataContext local:MonitorPageViewModel / /TextBlock.DataContext Run Text{Binding CurrentTime, ModeOneWay, StringFormatHH:mm:ss} FontFamily{StaticResource DigitalDisplay} FontSize30/ Run Text / Run Text{Binding CurrentTime, ModeOneWay, StringFormatdddd, ConverterCulturezh-CN} FontSize13/ /TextBlock !-- Other elements -- /Grid !-- Other columns -- /Grid /UserControl
### ViewModel 实现
保持 MonitorPageViewModel 和之前一样
csharp using System; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows.Threading;
public class MonitorPageViewModel : INotifyPropertyChanged { private DateTime _currentTime; private DispatcherTimer _timer; public MonitorPageViewModel() { _currentTime DateTime.Now; _timer new DispatcherTimer(); _timer.Interval TimeSpan.FromSeconds(1); _timer.Tick (s, e) CurrentTime DateTime.Now; _timer.Start(); } public DateTime CurrentTime { get _currentTime; set { _currentTime value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string name null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } }
### 总结
通过上述方法可以仅在需要的 TextBlock 上设置局部 DataContext这样不会影响 UserControl 的全局 DataContext并且可以保留原本绑定到 MainViewModel 的其他属性。