У меня есть простое текстовое поле + сценарий диалога с файлом.текстовое поле привязано к объекту в цвете.я хочу выбрать файл и заполнить его текстовым полем, что, в свою очередь, обновит свойство связанного объекта.удалось получить имя файла в текстовое поле, но затем привязка текстового поля не сработала, поскольку не обнаружила измененияМне пришлось добавить изменение фокуса (), чтобы вызвать обновление.есть ли лучший способ?
<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();
}
}