C# 自定义控件-没有预定义内容WPF/c的Listview#
我第一次使用了自定义控件(wpf)。以下是我想要的: 我想要一个带有自定义ListViewItem的自定义ListView(挑战是我不知道“表单”上有什么,我只知道它是一个C# 自定义控件-没有预定义内容WPF/c的Listview#,c#,wpf,custom-controls,C#,Wpf,Custom Controls,我第一次使用了自定义控件(wpf)。以下是我想要的: 我想要一个带有自定义ListViewItem的自定义ListView(挑战是我不知道“表单”上有什么,我只知道它是一个面板——我称之为“表单”ListViewItem上有什么) 我有一个CustomListView和一个CustomListViewItem: 编辑:CustomListView.cs 公共类CustomListView:ItemsControl { public static readonly dependencProper
面板
——我称之为“表单”ListViewItem上有什么)
我有一个CustomListView和一个CustomListViewItem:
编辑:CustomListView.cs
公共类CustomListView:ItemsControl
{
public static readonly dependencProperty ListContentProperty=dependencProperty.Register(“ListContent”、typeof(IList)、typeof(CustomListView)、new PropertyMetadata());
公共IList列表内容
{
获取{return(IList)GetValue(ListContentProperty);}
set{SetValue(ListContentProperty,value);}
}
公共CustomListView(ContentControl ContentControl,IList列表)
{
OverrideMetadata(typeof(CustomListView),new FrameworkPropertyMetadata(typeof(CustomListView));
DataContext=list;
ItemsSource=列表;
CustomListViewItem CustomListViewItem=新CustomListViewItem();
customListViewItem.Content=contentControl;
字符串xamlTemplate=XamlWriter.Save(customListViewItem);
字符串模板=
"" +
xamlTemplate+
"";
ItemTemplate=(DataTemplate)XamlReader.Parse(template);
SetValue(TemplateContentProperty、contentControl);
}
}
编辑:CustomListViewItem.cs
公共类CustomListViewItem:ContentControl
{
public static readonly dependencProperty MyEventProperty=dependencProperty.Register(“MyEvent”,typeof(EventHandler),typeof(CustomListViewItem));
public static readonly dependencProperty ListContentItemProperty=dependencProperty.Register(“ListContentItem”、typeof(IList)、typeof(CustomListViewItem)、new PropertyMetadata());
公共IList ListContentItem
{
get{return(IList)GetValue(ListContentItemProperty);}
set{SetValue(ListContentItemProperty,value);}
}
公共事件处理程序MyEvent
{
get{return(EventHandler)GetValue(MyEventProperty);}
set{SetValue(MyEventProperty,value);}
}
公共CustomListViewItem()
{
DefaultStyleKey=typeof(CustomListViewItem);
}
//我的按钮上的事件的一些方法
CustomListView的DataContext
是一个可观察的集合。当我直接在CustomListViewItem上创建“表单”时,一切都很完美。我可以使用按钮进行排序,添加一个新的空表单
现在我的问题来了
假设我们有一个类Person(只有一个名字),它实现了INotifyPropertyChanged
。我们创建了一些“Person”,并将它们添加到observeCollection
。在一个新的xaml元素上,我们创建了一个模板(PersonEditor)和一个带有一些绑定的“Form”
我希望在MainWindows上执行以下操作:
CustomListView CustomListView=new CustomListView(new PersonEdit(),\u person);
myContentControl.Content=customListView;
其中_person是可观察集合
。我们将表单模板提供给CustomListViewItem,并将其添加到ContentControl的内容中
编辑:Generics.xaml:
结果是,我只有一个元素具有良好的模板,其他元素已创建但为空
我认为问题来自绑定。但我不知道该怎么办。我尝试使用xamlReader/xamlLoad()创建一个新的“表单”,但再次使用绑定并不好。我还尝试克隆xaml,但再次尝试,没有绑定,按钮上没有事件
我不知道我还能做些什么来解决这个问题?这是我第一次使用自定义控件,所以如果我犯了错误(任何错误),请分享
编辑
我认为我的习惯现在更好了,我遵循了DataTemplate
和ContentControl
与xamlReader/xamlLoad混合的想法。绑定很好(与事件相同)。但是,为什么我以前使用它时(像内容而不是模板)绑定不好?我不知道xamlReader/xamlLoad是否是一种好的方法(我的意思是它看起来更像一个骗子).您需要对ContentPresenter
使用DataTemplate
,而不是ContentControl
。但是,您可能不应该一开始就这样做。只需使用ItemsControl
。首先,我在CustomListView上使用ItemsControl,但我无法获得我想要的事件。因此,我创建了CustomListViewItem。我尝试添加一个ContentPresenter,但我看不出我必须对DataTemplate做什么。你不应该在项目上需要一个事件。这应该是你的ViewModel的一部分。好吧,也许我没有像我想象的那样使用自定义控件。我想要项目上的事件,因为我希望它们在外部有按钮(像箭头一样向上指向它的位置。)