简体   繁体   English

MasterDetails自定义控件-WPF

[英]MasterDetails Custom Control - WPF

I want to create a reusable custom WPF control that will do the following: 我想创建一个可重复使用的自定义WPF控件,该控件将执行以下操作:

I want it to expose Property of MasterContent and property of ExpandedDetails. 我希望它公开MasterContent的属性和ExpandedDetails的属性。 When I run the application I want it to display the deatails of only The MasterControl the same as data grid does. 当我运行该应用程序时,我希望它仅显示MasterControl的详细信息,就像显示数据网格一样。 When I click on an item, I want it to expand and show the details of ExpandedDetails. 当我单击一个项目时,我希望它展开并显示ExpandedDetails的详细信息。

I will give an example: Suppose I have a customer class and it has Orders Property. 我举一个例子:假设我有一个客户类,并且它具有Orders属性。

When the Control is shown, it will display: 显示控件时,它将显示:

(Ok, I still can't post images, but I think it is clear from the explanation) (好吧,我仍然无法发布图片,但是我认为从说明中可以很清楚地看出来)

Now, suppose each row can be selected. 现在,假设可以选择每一行。 If I select Row number 2, row 2 will exapnd and show the orders of Person describes in that row. 如果我选择第2行,则第2行会扩展并显示该行中Person描述的顺序。 The expanding part will be shown between rows 2 and 3. When I click on it again, the order detais will disapear so there is no gap between row 2 and 3. 扩展部分将显示在第2行和第3行之间。当我再次单击它时,订单将消失,因此第2行和第3行之间没有间隙。

In my case, Master Content Property will be the persons details without the orders, and the ExpandedDetails will be the Orders by themselves. 在我的情况下,主内容属性将是没有订单的人员详细信息,而ExpandedDetails将是订单本身。

Can you please direct me to how to start implementing this or give me some good reading material. 您能否指导我如何开始实施此方法或给我一些很好的阅读材料。 I'm familiar with The master Details Pattern: I know how select Person and then show his orders in some seperated DataGrid, but how do I implement this as a reusable control, especailly the expanding part. 我熟悉Master Details Pattern:我知道如何选择Person,然后在单独的DataGrid中显示他的命令,但是我如何将其实现为可重用控件,尤其是扩展部分。 Thanks! 谢谢!

As far as I can interpret what you need, you might not even need a Custom Control for this. 据我所知,您甚至可能不需要自定义控件。 You can take a look at this CodeProject article , it will give you a sample of how to use RowDetailTemplate and how to use it in context of a DataGrid. 您可以看一下此CodeProject文章 ,它将为您提供如何使用RowDetailTemplate以及如何在DataGrid上下文中使用它的示例。 That way you can have a DataGrid to show your main data, and use the RowDetails to display something entirely different, yet dependent on the selection in your main DataGrid. 这样,您可以有一个DataGrid来显示您的主数据,并使用RowDetails来显示完全不同的内容,但取决于您在主DataGrid中的选择。

If you look for it in that project, it looks roughly like this: 如果您在该项目中寻找它,它的外观大致如下:

<DataGrid Name="dataGrid1"                
    RowDetailsTemplate="{StaticResource RowDetailTemplate}" >
    <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <ToggleButton x:Name="RowHeaderToggleButton"
                Click="ToggleButton_Click"
                Cursor="Hand" />
        </DataTemplate>
    </DataGrid.RowHeaderTemplate>
</DataGrid>       

I don't exactly understand what you mean behind the 我不完全了解您背后的意思

Property of MasterContent and property of ExpandedDetails MasterContent的属性和ExpandedDetails的属性

You mean that your Control will have those Properties in code, or "Properties" as what they are supposed to show? 您的意思是您的控件将在代码中包含这些属性,或者应该显示为“属性”? Because in the first case you will have to set the ExpandedDetails on any change of focus - i think that's not what you really mean. 因为在第一种情况下,您必须在焦点的任何更改上都设置ExpandedDetails-我认为这并不是您真正的意思。

Usually such behaviour(as it was already answered) does not require any specific control - you could just use container control( LisBox etc.) with custom data templates. 通常,这种行为(如已经回答的那样)不需要任何特定的控件-您可以仅将容器控件( LisBox等)与自定义数据模板一起使用。

And in such template you could either use some expanding container like Expander or implement your own triggers on datatemplate(here are some examples WPF Trigger for IsSelected in a DataTemplate for ListBox items ). 在这种模板中,您可以使用诸如Expander类的Expander容器,也可以在datatemplate上实现自己的触发器(这是ListBox项目的DataTemplate中的IsSelected的WPF触发器示例)。

With such triggers you could even implement some lazy UI initialization of the ExpandedDetails(with Visibility the controls will be in the UI tree for each of your Orders). 使用这样的触发器,您甚至可以实现ExpandedDetails的一些惰性UI初始化(具有Visibility ,控件将在每个订单的UI树中)。 Just don't bother yourself with such intricacies until your project will really need it. 只是在项目真正需要它之前,不要为此类复杂问题而烦恼。 There are better ways to reduce performance problems(like already embedded UI Virtualization - Virtualizing an ItemsControl? ). 有更好的方法来减少性能问题(例如已经嵌入的UI虚拟化- 虚拟化ItemsControl? )。

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

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