簡體   English   中英

WPF中的繼承

[英]Inheritance in WPF

我想讓WPF控件像這樣繼承,

public class BaseView : UserControl
{
   ...  
}    

public class BaseListView : BaseView
{
   ...
}

public class TeachersListView: BaseListView
{
}

public class StudentsListView : BaseListView
{
}

這里的“ BaseListView”是基類。 此類可能具有多種功能,具體取決於“ BaseListView”中存在的ListView。 我想將此“ BaseListView”繼承到幾個視圖,這些視圖可能會添加多個列並具有不同的數據綁定。 所以我的要求是

class BaseListView : BaseView
{
  This class will have the UI parts like commandStrip and followed by "Empty ListView".

  This "ListView" may not hold any columns in it.
}    

class StudentsListView:  BaseListView
{    
   In **XAML** part, Columns and its appropriate Data Binding will be added. I need to access the controls in .cs file. so that i can access the controls.

  void FindAndHighlightColumn()
   {
     // get the columns to find and search the list view and highlight.
   }
}

我怎樣才能做到這一點,正確的方法是什么。

我認為您想使用某些基本功能(如FindAndHighlightColumn())創建UserControl(Items或ContentControl)。

您可以創建“ lookless”的BaseListView,然后使用ControlTemplates來獲得幾種樣式(創建“ StudentListViewStyle”等樣式,每種樣式都帶有適當的ControlTemplate)。 ControlTemplate是一個視圖,因此您可以在每個模板中指定不同的綁定,以按其名稱訪問控件,您需要使用某些名為“ PART_XY”等的部分定義一個約定,以標准的ProgressBar-Control為例。

您是否正在使用Model-View-ViewModel體系結構? 閱讀完您的問題后,我最初的想法是,在您考慮的范圍內,我不會使用繼承。 相反,我會考慮組成。

例如,您是否可以通過組合多個用戶控件的.xaml文件來創建復合的StudentListView? 也許一個用戶控件顯示您的commandStrip,另一個用戶控件顯示適當的ListView(例如,StudentListViewUserControl,TeacherListViewUserControl等)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM