我试图做一个非常简单的用户控件具有您可以键入文本框,或者您可以通过点击浏览按钮找到一个路径。 我试图用一个依赖属性来做到这一点,但是在绑定到它时这并不完全工作。WPF路径TextBox控件以及浏览按钮,用户控件
这里我的XAML:
<UserControl x:Class="PathSelector.PathSelector"
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:PathSelector">
<DockPanel Height="28">
<Button DockPanel.Dock="Right" Padding="5" Margin="5 0 0 0"
FontWeight="Bold"
Content="..."
Click="BrowseButton_Click" />
<Grid>
<TextBox
HorizontalAlignment="Stretch" VerticalAlignment="Center"
x:Name="SelectedPathTxtBox"
LostKeyboardFocus="SelectedPathTxtBox_LostKeyboardFocus" />
</Grid>
</DockPanel>
</UserControl>
这是隐藏代码:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace PathSelector
{
/// <summary>
/// A simple input for path, with browse button
/// </summary>
public partial class PathSelector : UserControl
{
public PathSelector()
{
InitializeComponent();
}
private void BrowseButton_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
fileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
SelectedPathTxtBox.Text = fileDialog.FileName;
}
}
#region Dependency Properties
public string SelectedPath
{
get { return (string)GetValue(SelectedPathProperty); }
set { SetValue(SelectedPathProperty, value); }
}
public static readonly DependencyProperty SelectedPathProperty =
DependencyProperty.Register(
"SelectedPath",
typeof(string),
typeof(PathSelector),
new FrameworkPropertyMetadata(new PropertyChangedCallback(SelectedPathChanged))
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
private static void SelectedPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MessageBox.Show("Changed!");
// How to update the values here??
}
#endregion
private void SelectedPathTxtBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
SelectedPath = SelectedPathTxtBox.Text;
}
}
}
我想用这个用户控件这样以后:
<pathselector:PathSelector
SelectedPath="{Binding PathToSomeFile}"/>
“PathToSomeFile” 是ViewModel中应该在两个方向上更新的字符串变量。
我该如何做到这一点?我错过了什么?
非常感谢!
'PathToSomeFile'是一个字段或属性? –
'PathToSomeFile'是在实现INotifyPropertyChanged – joerg