gpt4 book ai didi

c# - 尝试绑定(bind)到同一用户控件的依赖属性时出错

转载 作者:行者123 更新时间:2023-11-30 23:25:30 25 4
gpt4 key购买 nike

我试图将我的 UserControl 绑定(bind)到我的 UserControl 本身中定义的依赖属性,但我不断收到绑定(bind)错误:

BindingExpression path error: 'DefinitionTypeResourceKeyProperty' property not found on 'object' ''DefinitionsList' (Name='')'. BindingExpression:Path=DefinitionTypeResourceKeyProperty; DataItem='DefinitionsList' (Name=''); target element is 'ItemsControl' (Name=''); target property is 'ItemTemplate' (type 'DataTemplate')

这是我的 xaml:

<UserControl x:Class="Editor.Common.DefinitionsList"
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"
xmlns:local="clr-namespace:Editor.Common"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<local:StringToResourceConverter x:Key="StringToResource" />
</UserControl.Resources>
<Expander>
<Expander.ContextMenu>
<ContextMenu>
<MenuItem Header="Add"
Command="{Binding AddItem}" />
<MenuItem Header="Remove"
Command="{Binding RemoveItem}" CommandParameter="{Binding SelectedItem}" />
</ContextMenu>
</Expander.ContextMenu>

<Grid>
<ItemsControl ItemsSource="{Binding Items}" ItemTemplate="{Binding DefinitionTypeResourceKeyProperty, Converter={StaticResource StringToResource}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}">
</ItemsControl>
</Grid>
</Expander>
</UserControl>

这是它的 .cs 文件:

using System.Windows;
using System.Windows.Controls;

namespace Editor.Common
{
public partial class DefinitionsList : UserControl
{
public string DefinitionTypeResourceKey
{
get { return (string)GetValue(DefinitionTypeResourceKeyProperty); }
set { SetValue(DefinitionTypeResourceKeyProperty, value); }
}

public static readonly DependencyProperty DefinitionTypeResourceKeyProperty =
DependencyProperty.Register("DefinitionTypeResourceKey", typeof(string), typeof(DefinitionsList));

public string Header
{
get { return (string)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}

public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(string), typeof(DefinitionsList));

public DefinitionsList()
{
InitializeComponent();
}
}
}

我将此控件用作通用列表控件,因此我可以在此列表中放置不同类型的控件。
这就是我尝试将它用于特定类型控制的方式:

<UserControl x:Class="Editor.Item.ItemEditorControl"
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"
xmlns:local="clr-namespace:Editor.Item"
xmlns:component="clr-namespace:Editor.Component"
xmlns:common="clr-namespace:Editor.Common"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="compListTemplate">
<component:ComponentListEditorControl />
</DataTemplate>
</UserControl.Resources>
<common:DefinitionsList Header="{Binding Name}" DefinitionTypeResourceKey="compListTemplate" />
</UserControl>

及其 .cs 文件:

使用 System.Windows.Controls;

namespace Editor.Item
{
public partial class ItemEditorControl : UserControl
{
public ItemEditorControl()
{
InitializeComponent();
DataContext = new ItemDefinitionViewModel();
}
}
}

为什么会出现此错误?

编辑:

我正在添加 ItemDefinitionViewModel 代码:

using Editor.Common;

namespace Editor.Item
{
public class ItemDefinitionViewModel : BaseViewModel<ItemEditorItem>
{
public ItemDefinitionViewModel()
{

}
}
}

及其基类:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;

namespace Editor.Common
{
public class BaseViewModel<T> : INotifyPropertyChanged
where T : new()
{
public event PropertyChangedEventHandler PropertyChanged;


public ICommand AddItem { get; private set; }
public ICommand RemoveItem { get; private set; }

public ObservableCollection<T> Items { get; private set; }

protected void RaisePropertyChangedEvent(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

public BaseViewModel()
{
Items = new ObservableCollection<T>();
AddItem = new RelayCommand(addItem);
RemoveItem = new RelayCommand(removeItem);
}

private void removeItem(object obj)
{
T removedItem = (T)obj;
Items.Remove(removedItem);
}

private void addItem(object obj)
{
T newItem = new T();
Items.Add(newItem);
}
}
}

最佳答案

您绑定(bind)到 DefinitionTypeResourceKeyProperty 而不是 DefinitionTypeResourceKey。您正在尝试绑定(bind)到 DP 定义而不是 DP。

“困扰”我的另一件事是您正在 RelativeSource RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}} 中寻找 UserControl。

相反,您应该查找 DefinitionsList。如果您更改它,编辑器会告诉您您在 DP 名称上输入错误,从而节省您的时间。

关于c# - 尝试绑定(bind)到同一用户控件的依赖属性时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37204416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com