gpt4 book ai didi

c# - UserControl 与模型的绑定(bind) - "property not found"

转载 作者:行者123 更新时间:2023-11-30 20:50:33 27 4
gpt4 key购买 nike

我做了一个 super 简单的用户控件来浏览文件

<UserControl x:Class="DrumMapConverter.FileBrowserTextBox"
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"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
mc:Ignorable="d" Height="24" d:DesignWidth="500">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Name="lblLabel" Text="{Binding Label, Mode=TwoWay}" MinWidth="150"/>
<Button Grid.Column="1" Content=" ... " Click="BrowseButton_Click"/>
<TextBox Grid.Column="2" Name="txtFilepath" Text="{Binding FilePath, Mode=TwoWay}"/>
</Grid>

</UserControl>

具有 2 个依赖属性:

标签和文件路径:

// FilePath
public static readonly DependencyProperty FilePathProperty =
DependencyProperty.Register("FilePath", typeof(string), typeof(FileBrowserTextBox), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(OnFilePathPropertyChanged)));

public string FilePath
{
get { return (string)GetValue(FilePathProperty); }
set { SetValue(FilePathProperty, value); }
}
static void OnFilePathPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var obj = o as FileBrowserTextBox;
if (obj == null)
return;
FileBrowserTextBox fileBrowserTextBox = (FileBrowserTextBox)o;
fileBrowserTextBox.txtFilepath.Text = (string)e.NewValue;
}
// Label
public static readonly DependencyProperty LabelProperty =
DependencyProperty.Register("Label", typeof(string), typeof(FileBrowserTextBox), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(OnLabelPropertyChanged)));
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
static void OnLabelPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var obj = o as FileBrowserTextBox;
if (obj == null)
return;
FileBrowserTextBox fileBrowserTextBox = (FileBrowserTextBox)o;
fileBrowserTextBox.lblLabel.Text = (string)e.NewValue;
}

然后在我的 MainWindow ctor 中我有这个

private DrumMapConverterDataModel model;
public MainWindow()
{
InitializeComponent();
model = new DrumMapConverterDataModel();
DataContext = model;
}

模型有 2 个属性:

private string inputFile = "";
public string InputFile
{
get { return inputFile; }
set {
inputFile = value;
OnPropertyChanged("InputFile");
}
}

private string outputFile = "";
public string OutputFile
{
get { return outputFile; }
set
{
outputFile = value;
OnPropertyChanged("OutputFile");
}
}

我像这样在 MainWindow.XAML 中绑定(bind)

<cust:FileBrowserTextBox  Label="Input File" FilePath="{Binding InputFile}"/>
<cust:FileBrowserTextBox Label="Output File" FilePath="{Binding OutputFile}"/>

运行它并得到这个错误

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“FileBrowserTextBox”(名称=“”)”上找不到“InputFile”属性。绑定(bind)表达式:路径=输入文件; DataItem='FileBrowserTextBox' (Name='');目标元素是“FileBrowserTextBox”(名称=“”);目标属性是“FilePath”(类型“String”)System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“FileBrowserTextBox”(名称=“”)”上找不到“OutputFile”属性。绑定(bind)表达式:路径=输出文件; DataItem='FileBrowserTextBox' (Name='');目标元素是“FileBrowserTextBox”(名称=“”);目标属性是“FilePath”(类型“String”)

这基本上意味着 UserControl 中没有 InputFile 和 OutputFile,但我试图将控件的 FilePath 属性与模型的 InputFile 和 OutputFile 绑定(bind),为什么不起作用?

提前致谢。

最佳答案

当你这样做的时候

DataContext="{Binding RelativeSource={RelativeSource Self}}"

FileBrowserTextBox 中,您覆盖了继承的 DataContext 更改绑定(bind)上下文。这意味着它将尝试在 FileBrowserTextBox 控件中查找 InputFileOutputFile 属性。删除该行并更改 FileBrowserTextBox 中的绑定(bind),这样它们就不会影响 DataContext,例如使用 RelativeSource ,类似于:

<TextBlock Grid.Column="0" Name="lblLabel" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=Label}" MinWidth="150"/>
<TextBox Grid.Column="2" Name="txtFilepath" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=FilePath}"/>

还在 PropertyChangedCallback 中为 LabelFilePath 你做:

fileBrowserTextBox.txtFilepath.Text = (string)e.NewValue;
fileBrowserTextBox.lblLabel.Text = (string)e.NewValue;

如果您只想更改 UI,则根本不需要处理属性更改回调。您已经在 XAML 中使用了绑定(bind),它应该为您执行此操作,采用绑定(bind)上下文很好,上面的行只会用固定值覆盖这些绑定(bind)

关于c# - UserControl 与模型的绑定(bind) - "property not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22441461/

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