WPF 用户控件依赖注入赋值

前言

我一直想组件化得去开发WPF,因为我觉得将复杂问题简单化是最好的

如何组件化开发

主窗口引用

xml 复制代码
<Window x:Class="WpfApp1.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:WpfApp1"
        xmlns:MD="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:Views="clr-namespace:WpfApp1.Views"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >
    <Window.Resources>
        <Style x:Key="my_text" TargetType="TextBlock">
            <Setter Property="FontSize" Value="30" />
            <Setter Property="Margin" Value="8" />
        </Style>
    </Window.Resources>
    <Window.DataContext >
        <!--需要命名来指定数据源-->
        <local:MainWindowViewModel x:Name="viewModel"/>
    </Window.DataContext>
    <Grid>
        <!--不能直接写TitleValue,Binding数据源会有问题-->
        <Views:ViewA Margin="10"
                Title="{Binding ElementName=viewModel,Path=TitleValue}" />
    </Grid>
</Window>

cs部分

csharp 复制代码
namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MainWindowViewModel
    {
        public string TitleValue { get; set; } =  "我是测试数据";

    }
}

控件窗口

xml 复制代码
    <UserControl.DataContext>
        <local:ViewAViewModel />
    </UserControl.DataContext>

    <Grid>
        <TextBlock Text="{Binding Title}" />
    </Grid>
csharp 复制代码
/// <summary>
/// ViewA.xaml 的交互逻辑
/// </summary>
public partial class ViewA : UserControl
{

    public static readonly DependencyProperty TitleProperty;

    /// <summary>
    /// 为了拿到数据源需要定义一下
    /// </summary>
    private ViewAViewModel ViewModel = new ViewAViewModel();
    public ViewA()
    {
        InitializeComponent();
        ViewModel = (ViewAViewModel)DataContext;

    }
    static ViewA()
    {
        //静态构造
        TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ViewA),new PropertyMetadata("",
            new PropertyChangedCallback((item, res) =>
            {
                //拿到数据,再次赋值
                var model =(ViewA)item;
                model.ViewModel.Title = (string)res.NewValue;
            })));
    }

    /// <summary>
    /// 只是为了有代码提示,添加依赖属性后不会被调用
    /// </summary>

    public string Title { get; set; }




}
public partial class ViewAViewModel : ObservableObject
{
    /// <summary>
    /// 通知更新
    /// </summary>
    [ObservableProperty]
    private string title = "ViewA Title!";

}
相关推荐
旋生万物4 小时前
【终极实战】用Python从零“生成“一个宇宙:螺旋干涉模型的代码实现
开发语言·前端·人工智能·react.js·php·wpf
Now喔8 小时前
WPF画刷介绍
wpf
李高钢15 小时前
WPF/WinForms 客户端通过 gRPC 与后端通信:从 Proto 契约到流式调用的完整指南
wpf·grpc·winforms
一见已难忘1 天前
基于 Harmony 6.0 应用的宠物寄养预约系统实现
wpf·宠物
fl1768312 天前
基于C#WPF实现的内存加速球清理类似360加速球内存清理
开发语言·c#·wpf
李高钢2 天前
【WPF】高级 UI 与性能优化实战:从卡顿到丝滑
ui·性能优化·wpf
Chris _data2 天前
WPF 上位机开发学习笔记 - 第四天
笔记·学习·wpf
李高钢2 天前
除了 IDE,C#/WPF 开发者每天离不开的 10 个工具
ide·c#·wpf
李高钢3 天前
用 JetBrains Rider 写 WPF 的 15 个高效技巧:从 VS 转过来也值得看
wpf
李高钢3 天前
【WPF】MVVM 与数据绑定实战:从原理到完整项目
wpf