局部 `DataContext`

为了确保的新 ViewModel 不会影响现有绑定到 `MainViewModel` 的其他属性,可以使用 `MonitorPage` 作为 `UserControl` 的局部 `DataContext`,而不覆盖整个 `UserControl` 的 `DataContext`。可以通过在 XAML 中的某个局部范围内(如包含时间显示的 `TextBlock`)设置局部 `DataContext` 来实现。

具体步骤如下:

1. **不要在整个 `UserControl` 设置 `DataContext`**,而是在需要显示时间的 `TextBlock` 上设置局部 `DataContext`。
2. **保留原有的 `DataContext`**,这样其他绑定会继续使用 `MainViewModel`。

### 修改后的 XAML

在包含时间显示的 `TextBlock` 上设置局部 `DataContext`,如下所示:

```xml
<UserControl x:Class="Zhaoxi.DigitaPlatform.Views.Pages.MonitorPage"
             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:Zhaoxi.DigitaPlatform.Views.Pages"
             xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
             xmlns:zxc="clr-namespace:Zhaoxi.DigitaPlatform.Components;assembly=Zhaoxi.DigitaPlatform.Components"
             xmlns:c="clr-namespace:Zhaoxi.DigitaPlatform.Common.Converter;assembly=Zhaoxi.DigitaPlatform.Common"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d" 
             d:DesignHeight="750" d:DesignWidth="1300">

    <UserControl.Resources>
        <!-- Your existing resources -->
    </UserControl.Resources>
    
    <!-- Your existing layout -->
    <Grid Margin="80,10,10,10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250"/>
            <ColumnDefinition/>
            <ColumnDefinition Width="250"/>
        </Grid.ColumnDefinitions>

        <!-- 第一列 -->
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="90"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <!-- 第一块 -->
            <Border CornerRadius="6">
                <Border.Background>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Color="#1116a1ff" Offset="0"/>
                        <GradientStop Color="Transparent" Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>
            </Border>
            <Grid VerticalAlignment="Top" Margin="0,10">
                <!-- Your existing layout -->
            </Grid>
            
            <!-- 在这里设置局部 DataContext -->
            <TextBlock VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,15"
                       Foreground="#555" Name="txt_time">
                <TextBlock.DataContext>
                    <local:MonitorPageViewModel />
                </TextBlock.DataContext>
                <Run Text="{Binding CurrentTime, Mode=OneWay, StringFormat='HH:mm:ss'}" FontFamily="{StaticResource DigitalDisplay}" FontSize="30"/>
                <Run Text="   "/>
                <Run Text="{Binding CurrentTime, Mode=OneWay, StringFormat='dddd', ConverterCulture=zh-CN}" FontSize="13"/>
            </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` 的其他属性。

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值