数据字节位改变

示例

1.数据模型

using CommunityToolkit.Mvvm.ComponentModel;
using System;

namespace WpfApp3.ViewModels
{
    public class MainViewModel:ObservableObject
    {
        private int val;
        public int Val 
        { 
            get=>val; 
            set 
            { 
                SetProperty(ref val, value); 
                HexStr = val.ToString("X4");
                BinStr = Convert.ToString(val, 2).PadLeft(8,'0');
            } 
        }
        private string hexStr;
        public string HexStr { get => hexStr;set => SetProperty(ref hexStr, value); }
        private string binStr;
        public string BinStr { get => binStr; set => SetProperty(ref binStr, value); }  
    }
}



2.View

<Window x:Class="WpfApp3.MainWindow"
        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:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal">
            <TextBlock Text="数据" FontSize="17" Padding="5" Margin="10 2"/>
            <TextBox x:Name="tb" Text="{Binding Val,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" FontSize="17" Foreground="Blue" Width="120" Margin="0 5" />
            <Button Content="第三位置1" Width="120" Margin="5" Height="35" Click="Btn1_Click" />
            <Button Content="第三位置0" Width="120" Margin="5" Height="35" Click="Btn2_Click" />
            <Button Content="第一位置1" Width="120" Margin="5" Height="35" Click="Btn3_Click" />
        </StackPanel>
        <StackPanel Grid.Row="1" Margin="10">
            <TextBlock FontSize="17">
                <Run Text="十六进制:" />
                <Run Text="{Binding HexStr}"  Foreground="Red"/>

            </TextBlock>
        </StackPanel>
        <StackPanel Grid.Row="2" Margin="10">
            <TextBlock FontSize="17">
                <Run Text="二进制:" />
                <Run Text="{Binding BinStr}" Foreground="Red"/>
            </TextBlock>
        </StackPanel>
    </Grid>
</Window>

Code.cs

using System.Windows;
using WpfApp3.ViewModels;

namespace WpfApp3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private MainViewModel main;
        public MainWindow()
        {
            InitializeComponent();
            main=new MainViewModel() { Val=0};
            DataContext = main;
        }

        private void Btn1_Click(object sender, RoutedEventArgs e)
        {
            main.Val = (int)(byte)(main.Val | 1 << 2);
        }

        private void Btn2_Click(object sender, RoutedEventArgs e)
        {
            main.Val=(int)(byte)(main.Val & ~(1 << 2));
        }

        private void Btn3_Click(object sender, RoutedEventArgs e)
        {
            main.Val = (int)(byte)(main.Val |(1 << 0));
        }
    }
}

image

posted @ 2026-09-01 20:35  丹心石  阅读(6)  评论(0)    收藏  举报