惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
小众软件
小众软件
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
Microsoft Security Blog
Microsoft Security Blog
Last Week in AI
Last Week in AI
H
Help Net Security
爱范儿
爱范儿
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
WordPress大学
WordPress大学
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
腾讯CDC

博客园 - 丹心石

锁定鼠标光标到指定屏幕 WPF 访问WebAPI Avalonia 避坑经验总结 Avalonia 窗口与配置 Avalonia 程序打包成Linux平台的运行程序 数据处理与转换 信号与槽 Qt 串口通信 Avalonia 设计时DataContext数据山下文和WPF设计时DataContext数据上下文 Avalonia Ursa 使用 Avalonia-消息对话框 Avalonia-数据绑定 Avalonia-样式 阿里图标库iconfont 在Avalonia 中的使用 Avalonia MVVM Avlonia 自定义控件 Sqlite EF For ConsoleCore Sqlite EF CodeFirst For WPF win10 环境变量不可编辑 WCF-双工通讯 MahMetro 框架学习 由于缺少调试目标“xxx”Visual Studio 无法开始调试。或者响应地设置outputpath和AssemblyName 属性 WPF 应用程序本地化-资源文件方式 FastReport 行高设置实现行高随内容自动变化
数据字节位改变
丹心石 · 2026-09-01 · via 博客园 - 丹心石

示例

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