C# WPF中关于DataContext数据绑定的设计实例设置

By jerrxjr1220 at 2023-08-25 • 0人收藏 • 286人看过

在C# WPF中在进行DataContext数据绑定时,经常会在设计器中报一些未找到绑定数据的错误,但在实际编译时又可以正常运行。

无标题.png

其主要原因是这些DataContext是在后台程序中生成的,在运行时才会编译并加载到前台界面,所以在编译前的设计器中是无法找到的。

解决方案是在前台界面中预先声明设计实例,这样不但可以去除绑定错误的提示,还可以直接预览可绑定的数据源。

例如,上述例子中的

d:DataContext="{d:DesignInstance Type=local:MainViewModel}"
<Window x:Class="WPFdemo01.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:WPFdemo01"
        xmlns:sys="clr-namespace:System;assembly=mscorlib" 
        d:DataContext="{d:DesignInstance Type=local:MainViewModel}"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

添加完以后

无标题1.png

完整的前台界面代码

<Window x:Class="WPFdemo01.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:WPFdemo01"
        xmlns:sys="clr-namespace:System;assembly=mscorlib" 
        d:DataContext="{d:DesignInstance Type=local:MainViewModel}"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
 
        <StackPanel Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Slider Name="sliderFontSize" Value="{Binding FontSize}" Width="300" Maximum="48"></Slider>
            <TextBlock Text="Test Size" FontSize="{Binding FontSize}" />
        </StackPanel>
    </Grid>
</Window>

后台ViewModel

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPFdemo01
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private int _fontsize;
        public int FontSize
        {
            get { return _fontsize; }
            set
            {
                if (_fontsize != value)
                {
                    _fontsize = value;
                    OnPropertyChanged(nameof(FontSize));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

主程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFdemo01
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = new MainViewModel() { FontSize = 16 };
        }

    }
}


登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...