WPF 文本框和浏览文件 - 有更优雅的解决方案吗?

发布于 2024-12-12 04:33:25 字数 1603 浏览 0 评论 0原文

我有一个简单的文本框+文件对话框场景。文本框绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-12-19 04:33:25

只需直接设置视图模型属性 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.

不可一世的女人 2024-12-19 04:33:25

TextBox 的默认更新位于 LostFocus 上。尝试将其更改为 PropertyChanged:

<TextBox Text="{Binding Path=FlexString1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />

The TextBox's default update is on LostFocus. Try changing it to PropertyChanged instead:

<TextBox Text="{Binding Path=FlexString1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文