【发布时间】:2014-03-16 19:06:03
【问题描述】:
我制作了一个超级简单的用户控件来浏览文件
<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 中绑定
<cust:FileBrowserTextBox Label="Input File" FilePath="{Binding InputFile}"/>
<cust:FileBrowserTextBox Label="Output File" FilePath="{Binding OutputFile}"/>
运行它并得到这个错误
System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“FileBrowserTextBox”(名称=“”)上找不到“InputFile”属性。绑定表达式:路径=输入文件; DataItem='FileBrowserTextBox' (Name='');目标元素是'FileBrowserTextBox'(名称='');目标属性是“文件路径”(类型“字符串”) System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“FileBrowserTextBox”(名称=“”)上找不到“OutputFile”属性。绑定表达式:路径=输出文件; DataItem='FileBrowserTextBox' (Name='');目标元素是'FileBrowserTextBox'(名称='');目标属性是“文件路径”(类型“字符串”)
这基本上意味着UserControl中没有InputFile和OutputFile,但我试图将控件的FilePath属性与我的模型的InputFile和OutputFile绑定,为什么不起作用?
提前致谢。
【问题讨论】:
标签: c# wpf xaml user-controls