
出于演示目的,我创建了简单的Item类示例,其中我有这样的项集合:
public class Item{ public string ItemnameToBeSureWhatPropertyIsBound { get; set; } } 在我的viewmodel中,我创建了这样的集合,并将其公开(有一个项目可以单独进行比较):
public class MainWindowviewmodel : INotifyPropertyChanged{ private ObservableCollection<Item> _items; private Item _exampleItem; public MainWindowviewmodel() { Items = new ObservableCollection<Item>(new[] { new Item { ItemnameToBeSureWhatPropertyIsBound = "Me" },new Item { ItemnameToBeSureWhatPropertyIsBound = "MySelf" },new Item { ItemnameToBeSureWhatPropertyIsBound = "Ich" },}); ExampleItem = Items.LastOrDefault(); } public ObservableCollection<Item> Items { get { return _items; } set { _items = value; OnPropertyChanged(); } } public Item ExampleItem { get { return _exampleItem; } set { _exampleItem = value; OnPropertyChanged();} } public event PropertyChangedEventHandler PropertyChanged; protected virtual voID OnPropertyChanged([CallerMembername] string propertyname = null) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this,new PropertyChangedEventArgs(propertyname)); }} 我的自定义用户控件定义如下:
<UserControl x:Class="WpfDataTemplate.ItemRowUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/Expression/blend/2008" mc:Ignorable="d" d:DesignHeight="40" d:DesignWIDth="300" x:name="ItemRowControl" DataContext="{Binding Mode=OneWay,relativeSource={relativeSource Self}}"> <GrID Background="Yellow" Height="40"> <TextBlock Text="{Binding Itemname}" Foreground="Black"/> </GrID></UserControl> …并且代码后面有一个DependencyProperty:
public partial class ItemRowUserControl : UserControl{ public ItemRowUserControl() { InitializeComponent(); } public static Readonly DependencyProperty ItemnameProperty = DependencyProperty.Register( "Itemname",typeof (string),typeof (ItemRowUserControl),new PropertyMetadata(default(string))); public string Itemname { get { return (string) GetValue(ItemnameProperty); } set { SetValue(ItemnameProperty,value); } }} 问题是,当我尝试在DataTemplate中为ItemsControl绑定Item的属性时,我正在MainWindow中这样做(注意:我有虚拟转换器仅用于调试目的,返回值,仅此而已):
<Window.DataContext> <my:MainWindowviewmodel /></Window.DataContext><Window.Resources> <my:MyDummyConverter x:Key="MyDummyConverter" /></Window.Resources><GrID> <GrID.RowDeFinitions> <RowDeFinition /> <RowDeFinition Height="50" /> </GrID.RowDeFinitions> <ItemsControl name="ItemsControl" ItemsSource="{Binding Items}" GrID.Row="0" Background="Red"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate DataType="{x:Type my:Item}"> <my:ItemRowUserControl Itemname="{Binding ItemnameToBeSureWhatPropertyIsBound,Converter={StaticResource MyDummyConverter}}" /> <!--<GrID Background="Pink"> <TextBlock Text="{Binding ItemnameToBeSureWhatPropertyIsBound,Converter={StaticResource MyDummyConverter}}" Foreground="Black" Height="30" /> </GrID>--> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> <GrID GrID.Row="1"> <my:ItemRowUserControl Itemname="{Binding DataContext.ExampleItem.ItemnameToBeSureWhatPropertyIsBound,Elementname=MyWindow,Converter={StaticResource MyDummyConverter}}" /> </GrID></GrID> 现在,如果我绑定到我的自定义ItemRowUserControl,我进入转换器的值(我在DeBUG Output中看到相同的)是ItemRowUserControl本身.但是,如果我绑定到注释掉的代码,一切正常.为什么会这样,我如何对DataTemplate进行自定义控制,以便绑定(由intellisense提供)可以工作?在旁注:绑定到我的ItemRowUserControl网格行1(在底部)工作正常,所以我猜控制设置为按预期工作?
解决方法 问题是您明确将UserControl的DataContext设置为自身:DataContext="{Binding Mode=OneWay,relativeSource={relativeSource Self}} 删除该赋值并编写Itemname绑定,如下所示:
<TextBlock Text="{Binding Itemname,relativeSource={relativeSource AncestorType=UserControl}}"/> 或者像这样
<TextBlock Text="{Binding Itemname,Elementname=ItemRowControl}"/> 总结 以上是内存溢出为你收集整理的c# – 绑定到DataConmplate for ItemsControl中的自定义控件全部内容,希望文章能够帮你解决c# – 绑定到DataConmplate for ItemsControl中的自定义控件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)