간단한 텍스트 상자 + 파일 대화 상자 시나리오가 있습니다. 텍스트 상자는 colooection의 객체에 바인딩됩니다. 파일을 선택하고 텍스트 상자를 채우면 바인딩 된 객체 속성이 업데이트됩니다. 텍스트 상자에 파일 이름을 가져올 수 있었지만 텍스트 상자 바인딩은 변경 사항을 감지하지 못하기 때문에 실행되지 않았습니다. 업데이트를 실행하기 위해 focus() 변경을 추가해야했습니다. 거기에 더 좋은 방법이 있습니까?WPF 텍스트 상자 및 파일 찾아보기 - 더 멋진 솔루션이 있습니까?
<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();
}
}