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!";

}
相关推荐
code bean1 天前
【WPF】WPF 项目实战:构建一个可增删、排序的光源类型管理界面(含源码)
wpf
沉到海底去吧Go1 天前
【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
ocr·wpf·图片识别改名·图片识别重命名·图片内容改名
lph19721 天前
自定义事件wpf
wpf
code bean1 天前
【WPF】从普通 ItemsControl 到支持筛选的 ItemsControl:深入掌握 CollectionViewSource 用法
wpf
碎碎念的安静2 天前
WPF可拖拽ListView
c#·wpf
界面开发小八哥2 天前
界面组件DevExpress WPF中文教程:Grid - 如何识别行和卡片?
.net·wpf·界面控件·devexpress·ui开发
TwilightLemon3 天前
WPF 使用CompositionTarget.Rendering实现平滑流畅滚动的ScrollViewer,支持滚轮、触控板、触摸屏和笔
wpf
Vae_Mars5 天前
WPF中自定义消息弹窗
wpf
Magnum Lehar5 天前
GameEngine游戏引擎前端界面wpf页面实现
前端·游戏引擎·wpf
TA远方6 天前
【C#】一个简单的http服务器项目开发过程详解
服务器·http·c#·wpf·web·winform·console