WPF 文本框和浏览文件 - 有更优雅的解决方案吗?
我有一个简单的文本框+文件对话框场景。文本框绑定到 colooection 中的一个对象。我想选择该文件并让它填充文本框,这反过来将更新绑定的对象属性。设法将文件名输入文本框,但随后文本框绑定没有触发,因为它没有检测到更改。我必须添加 focus() 更改才能触发更新。有更好的办法吗?
<TextBox Text="{Binding Path=FlexString1,Mode=TwoWay}"
Height="23"
HorizontalAlignment="Left"
Margin="10" Name="textPath"
VerticalAlignment="Top"
Width="236" />
<Button Height="25"
HorizontalAlignment="Left"
Margin="0"
Name="btnBrowseFile"
Padding="1" VerticalAlignment="Top"
Width="45" Click="btnBrowseFile_Click">
<TextBlock FontSize="10"
FontWeight="Normal"
Foreground="#FF3C3C3C"
Text="Browse"
TextWrapping="Wrap" />
</Button>
private void btnBrowseFile_Click(object sender, RoutedEventArgs e)
{
// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
//dlg.FileName = "Document"; // Default file name
//dlg.DefaultExt = ".txt"; // Default file extension
//dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
TextBox path = (TextBox)(((FrameworkElement)sender).Parent as FrameworkElement).FindName("textPath");
path.Text = dlg.FileName;
path.Focus(); //these 2 lines force the binding to trigger
((Button)sender).Focus();
}
}
i have a simple textbox + file dialog scenario. the textbox is bound to an object in a colooection. i want to select the file and have it populate the text box, which in turn will update the bound object property. managed to get the filename into the textbox, but then the textbox binding was not firing because it didn't detect a change. i had to add a focus() change to trigger the update. is there a better way?
<TextBox Text="{Binding Path=FlexString1,Mode=TwoWay}"
Height="23"
HorizontalAlignment="Left"
Margin="10" Name="textPath"
VerticalAlignment="Top"
Width="236" />
<Button Height="25"
HorizontalAlignment="Left"
Margin="0"
Name="btnBrowseFile"
Padding="1" VerticalAlignment="Top"
Width="45" Click="btnBrowseFile_Click">
<TextBlock FontSize="10"
FontWeight="Normal"
Foreground="#FF3C3C3C"
Text="Browse"
TextWrapping="Wrap" />
</Button>
private void btnBrowseFile_Click(object sender, RoutedEventArgs e)
{
// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
//dlg.FileName = "Document"; // Default file name
//dlg.DefaultExt = ".txt"; // Default file extension
//dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
TextBox path = (TextBox)(((FrameworkElement)sender).Parent as FrameworkElement).FindName("textPath");
path.Text = dlg.FileName;
path.Focus(); //these 2 lines force the binding to trigger
((Button)sender).Focus();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需直接设置视图模型属性
FlexString1
即可。绑定将确保 UI 正确更新。
您还可以将浏览对话框放在命令上,以便从视图模型而不是视图中完成。
Just set the view model property
FlexString1
directly.The binding will ensure that the UI gets updated correctly.
You could also put the browse dialog on a command so it's done from within the view model rather than the view.
TextBox 的默认更新位于 LostFocus 上。尝试将其更改为 PropertyChanged:
The TextBox's default update is on LostFocus. Try changing it to PropertyChanged instead: