I'm using MVVM to load text files and to show their content.
Model
MyFile.cs has a Name and Text // Implements the INotifyPropertyChanged
MyFileRepository.cs // collection of my loaded files
ViewModel
OpenFileCommand to load a file and add it to the _filerepository object
FileCollection that's bound to the View
View
Button to fire the OpenCommand
ComboBox to show the names of the loaded files
TextBox to show the content of selected file in combobx
<Button Name="OpenFile" Command="{Binding OpenFileCommand}">
<ComboBox Name="FilesList" ItemsSource="{Binding Path=FileCollection}" DisplayMemberPath="Name" />
<TextBox Name="FileContent" Text="{Binding the Text of selected file in combobx "/>
How to bind the Text property of MyFile selected in combobx to the TextBox?
The easiest approach would be element binding:
<TextBox Name="FileContent"
Text="{Binding SelectedItem.Text,ElementName=FilesList} />
So that's binding to the Text property of the SelectedItem in your FilesList ComboBox, which (if everything's wired up the way I think it is) is of type MyFile.
Without element binding you could add the property "SelectedItem" (Type: MyFile) to your VM and bind it to the SelectedItem property of your combobox (mode=twoway).
Now your TextBox.Text-Property should look like:
<TextBox Name="FileContent"
Text="{Binding SelectedItem.Text} />
Related
There are two TextBox on the WPF page. While user is typing into the first TextBox, the second TextBox must display modified text from the first TextBox. As a result both values must be binded to a view model.
Can it be done with Data Binding?
I was able to get the second TextBox display text from the first one implementing DependencyProperty on my view model. But I don't have the slightest idea how to apply transformation on the fly.
Maybe there is an easy way to achieve this?
Just use databinding with UpdateSourceTrigger set to PropertyChanged:
<TextBox x:Name='txt1' Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name='txt2' Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
In this case it will update ViewModel's property on the fly instead of waiting for FocusLost.
I have a ComboBox in my wpf application.
It's ItemsSource is binded to some table in my DataSet.
I need the text property to be binded to another's object property . I doesn't work because the ComboBox doesn't want to get two DataContexts. How can I solve this problem?
<StackPanel Width="Auto" Height="Auto" MinWidth="296" Orientation="Vertical" x:Name="MyStackPanel">
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding}" Text={Binding Path=MyProperty} />
</StackPanel>
In the code behind :
MyComboBox.DataContext = MyDataSet.Tables[MyTable];
MyStackPanel.DataContext = MyObject;
I want the ComboBox to show items from one DataContext but to show the text from another DataContext. How can I do it?
Don't use DataContext. Set the Source property of your bindings in XAML or create the bindings in code and set the Source property there.
Why are you assigning something to the datacontext of the stackpanel? From the looks of it, its not used.
Your code should work if MyDataSet.Tables[MyTable] returns an enumeration and contains a property called MyProperty.
What do you mean when you say that the combobox "doesn't want to get two DataContexts"?
Look into the properties IsEditable and IsReadOnly of the combobox.
Something like
<ComboBox x:Name="MyComboBox" ItemsSource="{Binding}" Text={Binding ElementName=MyStackPanel Path=DataContext.MyProperty} />
I have a combo box (in my wpf-mvvm app). I have set IsEditable = true. But the "property changed event" is getting fired when I start typing.
How can I set UpdateSourceTrigger = Propertychanged here ?
Also..I need to call a validation function if user has entered new value ( i mean other than those available in list ..using edit functionality).
Any help will be appreciated.
<ComboBox ItemsSource="{Binding Path = PlanTypeBasedContractNumberList }" Width="90" IsEditable="True"
SelectedValue="{Binding GeneralCharacteristicsDataContext.ContractNumber.Value}">
</ComboBox>
In an editable ComboBox, the SelectedItem and SelectedValue properties refer to the Popup items, not the editable item. Once you start typing, the SelectedItem becomes "unselected" and that's why the event fires.
To bind to the value of the TextBox of the ComboBox, use the Text property:
<ComboBox IsEditable="True" Text="{Binding Path=..., UpdateSourceTrigger=...}">
The listbox contain images and the object assigned to the listbox have corresponding wmv file path. How to bind the selected path the the mediaelement through {binding}.
Geetha.
You can use ElementName to set the binding source to the list box, and then bind to a property of SelectedItem:
<ListBox Name="imageListBox" ... />
<MediaElement Source="{Binding ElementName=imageListBox, Path=SelectedItem.WmvPath}"/>
in my MVVM application (in wpf)
i have two view and I want to bind the context of my label on my textbox value (in the other view)
SelectorView.xaml contain this control:
<TextBox x:Name="tbArt" value="XX"/>
DescriptionView.xaml contain this control:
<label context="{binding on the tbArt value????}">
Is that possible directly without going in code behing and viewmodels ?
Will the label be refresh automatically ?
Thanks !
If both of the controls databinds to the same property the label will refresh when the value is changed.Make sure that the property triggers property changed when it gets changed.
ex:
in XAML.
<TextBox x:Name="tbArt" value="{Binding Path=TheProperty, UpdateSourceTrigger=PropertyChanged}"/>
<label context="{binding TheProperty}">
in the textbox make sure you use:
UpdateSourceTrigger=PropertyChanged
. otherwise the property won't be changed until focus is shifted from the textbox.
If all you want to do is display the value of one control in another control within the same window/page, you could do the following:
<TextBox x:Name="tbArt" Text="XX" />
<Label Content="{Binding Path=Text", ElementName=tbArt}" />
This will bind the content of the Text of the label to a control with the name "tbArt". You can do the same thing with other properties of the control as well.
For instance,
<TextBox x:Name="tbArt" Text="XX" Width=33 />
<Label Content="{Binding Path=Width, ElementName=tbArt}" />
will display "XX" in the textbox and "33" in the label.