Jan*_*kan 6 c# data-binding wpf custom-controls itemscontrol
我有绑定的问题DataTemplate基础上定义DataType的ItemsControl,当我想结合我的自定义用户控件.
出于演示目的,我创建了简单的Item类示例,其中我有这样的项集合:
public class Item
{
public string ItemNameToBeSureWhatPropertyIsBound { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的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));
}
}
Run Code Online (Sandbox Code Playgroud)
我的自定义用户控件定义如下:
<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>
Run Code Online (Sandbox Code Playgroud)
......它有一个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); }
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试绑定到DataTemplate中的ItemsControl的,这我在做项目中的属性主窗口像这样的(注:我只是用来调试,返回值回虚拟转换器,仅此而已):
<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>
Run Code Online (Sandbox Code Playgroud)
现在,如果我绑定到我的自定义ItemRowUserControl,我进入转换器的值(我在Debug Output中看到相同的)是ItemRowUserControl本身.但是,如果我绑定到注释掉的代码,一切正常.为什么会这样,我如何对DataTemplate进行自定义控制,以便绑定(由intellisense提供)可以工作?在旁注:绑定到我的ItemRowUserControl网格行1(在底部)工作正常,所以我猜控制设置为按预期工作?
问题是您明确将DataContextUserControl 设置为自身:
DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}
Run Code Online (Sandbox Code Playgroud)
删除该赋值并写下这样的ItemName绑定:
<TextBlock Text="{Binding ItemName,
RelativeSource={RelativeSource AncestorType=UserControl}}"/>
Run Code Online (Sandbox Code Playgroud)
或者像这样
<TextBlock Text="{Binding ItemName, ElementName=ItemRowControl}"/>
Run Code Online (Sandbox Code Playgroud)