开发者

Silverlight FileInfo DataContext for TextBox

I have a textbox in silverlight that is defined as follows.

<TextBox x:Name="hedtextBox" DataContext="{Binding HedFileInfo}" Text="{Binding Name}" Width="100" Grid.Column="1" Margin="2,0,2,0" />

My viewmodel has a FileInfo object called HedFileInfo. And, I have a Button beside the textbox which, when clicked, launches the OpenFileDialog as follows

    private void HedBrowseButtonClick(object sender, RoutedEventArgs e)
    {
        var ofd = new OpenFileDialog { Filter = "CSV Files (.csv)|*.csv" };
        var res开发者_StackOverflow社区ult = ofd.ShowDialog();
        // Process open file dialog box results
        if (result != true) return;
        hedTextBox.DataContext = ofd.File;
    }

I'm doing all this because silverlight doesnt let me access the FullName in OpenFileDialog for all paths(network drives etc). And if I just bind a string in the ViewModel to the TextBox, I wont be able to get the full path in the string. The TextBox text binds to the Name field, but when I set the DataContext in the button click event, the FileInfo in the viewmodel is not updating Does this make sense?


I think you've come to the correct answer yourself. If you want the property on the Source object (the HedFileInfo) to change when the DataContext property is modified you need to change the binding mode to TwoWay. This is perfectly normal and how its supposed to work.

Consider the most basic of bindings:-

 <TextBox Text="{Binding Description}" />

This code has set a binding on the TextBox object for its TextProperty dependency propery. Whenever the Description property changes this binding will assign the value of Description to the TextBox.Text property. However there are two other ways to set the Text property. The user can type over what it is currently present or code can assign a value to the Text property. It is therefore possible for the TextBox.Text property to have a different value from the Description. In the default OneWay mode the Description property on the source object is not modified.

Now change to:-

 <TextBox Text="{Binding Description, Mode-TwoWay}" />

Whenever the user enters new text or code modifies the Text property directly the binding will now respond by updating the Description property on the source object.

The DataContext property and the binding thereof behaves exactly the same way. With:-

 <TextBox DataContext="{Binding HedFileInfo}" Text="{Binding Name}" />

Whenever the value of HedFileInfo changes in the source view model (assuming INotifyPropertyChanged is implemented) then the value of DataContext will be changed. However when code modifies the value of DataContext directly the binding (being in OneWay mode) does not update the source view model.

Change it to this:-

 <TextBox DataContext="{Binding HedFileInfo, Mode=TwoWay}" Text="{Binding Name}" />

and now it works as you would like it to. When code changes the DataContext value directly the binding will respond by updating the HedFileInfo property of the source view model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜