简体   繁体   English

在WPF / C#中具有绑定的外部UserControl

[英]External UserControl with Bindings in WPF/C#

So, I have an UserControl which is basically a Grid with 3 different DataGrids and some Labels. 因此,我有一个UserControl,它基本上是一个具有3个不同的DataGrid和一些标签的网格。 Seeing how I need to use this 3 times, instead of copying and pasting the code, I thought I'd just generate it once and use it in my main window. 看到我需要如何使用3次,而不是复制和粘贴代码,我想我只需生成一次并在主窗口中使用它。

I have defined the UserControl as: 我已经将UserControl定义为:

<UserControl x:Class="Propuestas.UI.Andrei.DGMTX"
             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:Propuestas.UI.Andrei"
             mc:Ignorable="d"
             Height="300" 
             Width="791.496">

And I am using it in my window as such: 我在窗口中这样使用它:

<StackPanel Grid.Row="2">
    <local:DGMTX/>
    <local:DGMTX/>
    <local:DGMTX/>
</StackPanel>

For some reason, it doesn't show up in the designer panel on my main window. 由于某种原因,它没有显示在我主窗口的设计器面板中。 Is there something I'm doing wrong? 我做错了什么吗?

Also, I would like to be able to bind based on a bound element. 另外,我希望能够基于绑定元素进行绑定。 For example, let's say I have a class Model which has all the data that I need to represent in my UserControl. 例如,假设我有一个Model类,其中包含我需要在UserControl中表示的所有数据。 I would like to do something like 我想做类似的事情

<local:DGMTX Binding = {Binding Model}/>

and then be able to bind all of the other elements in my UserControl in its code. 然后可以在其代码中绑定UserControl中的所有其他元素。 Is there a way I could do this in XAML? 有没有办法可以在XAML中做到这一点? Or do I have to do it programmatically? 还是我必须以编程方式进行?

There are two ways to communicate your view model to controls: 有两种方法可以将视图模型与控件进行通信:

  1. As one commenter suggested, bind your view model to the data context of the user control. 正如一位评论者所建议的,将您的视图模型绑定到用户控件的数据上下文。 This enables binding everything in your view model to the inner workings of the control. 这样可以将视图模型中的所有内容绑定到控件的内部工作。 Problem is the inner workings now depend on the data the object is associated with. 问题在于内部工作方式现在取决于对象与之关联的数据。

  2. Create dependency properties for only the ones in your view model that the user control actually needs. 仅为视图模型中用户控件实际需要的属性创建依赖项属性。 I personally prefer this over the first in almost 99% of all cases because you know exactly what data the control expects and you can manipulate bound data in ways unique to the control that maybe the view model isn't responsible for. 在所有案例中,我个人都比第一个更喜欢这种情况,因为您完全知道控件需要什么数据,并且可以以视图模型不负责的方式以控件特有的方式操作绑定数据,因此,我个人最喜欢这种情况。

Couple things to note about designer support when creating your own controls: 创建自己的控件时,需要注意有关设计师支持的几件事:

  1. Visual Studio's designer still has a lot of issues when it comes to WPF. 对于WPF,Visual Studio的设计师仍然有很多问题。 Don't believe me? 不相信我吗? Try referencing a dynamic resource defined in your main assembly in another. 尝试在另一个主程序集中引用动态资源。 The designer will crash and tell you it can't be found. 设计器将崩溃,并告诉您找不到它。 This isn't the actual case, however. 但是,这不是实际情况。 As soon as you run the app, you will never see this exception. 一旦运行该应用程序,您将永远不会看到此异常。
  2. In order to see changes made to source reflect in designer, you have to build the project first (the project in which the control resides, not necessarily the one it's referenced in). 为了查看对源所做的更改反映在设计器中,您必须首先构建项目(控件所在的项目,不一定是其引用的项目)。 Sometimes, "cleaning" or (with better luck in some cases) "rebuilding" the project is the only thing that updates the designer in the main project when "building" doesn't work. 有时,“清理”或(有时运气更好)“重建”项目是在“构建”不起作用时唯一更新主项目中设计师的事情。
  3. If after considering the latter and you still can't see anything, consider the implementation of the control. 如果在考虑了后者之后仍然看不到任何内容,请考虑该控件的实现。 Is anything out of place? 有什么地方不对吗? Did something accidentally get hidden? 有什么意外藏起来吗? You may not think so at first and maybe it takes ten hours of frustration to succumb and check, but the little things can make all the difference. 起初您可能并不这么认为,也许屈服和检查可能要花十个小时的挫败感,但这些小事情可以使一切变得不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM