C# WPF中关于DataContext数据绑定的设计实例设置
By
jerrxjr1220
at 2023-08-25 • 0人收藏 • 341人看过
在C# WPF中在进行DataContext数据绑定时,经常会在设计器中报一些未找到绑定数据的错误,但在实际编译时又可以正常运行。
其主要原因是这些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">
添加完以后
完整的前台界面代码
<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 }; } } }
登录后方可回帖