I have a RadioButton element whose IsChecked property is bound to MyProperty in ViewModel. The Binding has mode OneWayToSource for some reasons, it pushes the value from RadioButton.IsChecked to ViewModel.MyProperty.
RadioButton.IsChecked is initially false, now. I want to set an initial value from ViewModel, which might be even true. I cannot do that because the property is occupied by the binding.
Is there any way to use Binding with that mode and set default value to the bound property in UI? Something like that:
<RadioButton IsChecked="{Binding MyProperty, Mode=OneWayToSource, DefaultVaule=???}">
</RadioButton>
If I understood you correct, I think this might help:
You can define the default value through the TargetNullValue property. You can define a FallbackValue value in case of error either, for example:
<TextBox Text="{Binding MyProperty, TargetNullValue=0, FallbackValue=10}" />
see here:
enter link description here
Related
I'd like to set up a binding in XAML that updates from either the source or target like a TwoWay binding. But when the DataContext is applied, I'd like the initial value to be taken from the Target like a OneWayToSource binding.
<TextBox Text="{Binding Memo}" />
How do I set up this binding so that the TextBox Text is updated from the Memo property of the DataContext, and vice-versa, but that the initial value on hookup is taken from the TextBox text?
Just don't assign a value to the Memo property. By default the value of TextBox.Text is null.
You can set the Binding.TargetNullValue to set a default value:
<TextBox Text="{Binding Memo, TargetNullValue=TextBox default unset value}" />
I am trying to get the bool value of the checkbox present in the Listview. I am binding to a bool public property "Assignm" in the view model. I tried the below binding pattern but the problem is if i select one checkbox it selects all checkboxes and vice versa. I think this is because relativesource is listview and it works on complete listview. I also tried changing the relative source to ListviewItem but that didn't trigger anything. Can someone help me please. Do i need to change something here ?
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding MU_Identifier}" IsChecked="{Binding DataContext.Assignm, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}">
</CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
Because your binding for IsChecked property is Assignm property which seems to be one property of your view model.
If there is a boolean property named Assignm for the data model of the DataSource of ListView, then just change the binding like this: {Binding Assignm}, as Tag property does.
All your items are bounded to a single property, so when one item changes a property in your context it changes on other items.
To provide correct work all your items from ItemsSource should have property IsChecked.
Check this Example
I have a comboxbox defined like this (basically):
<ComboBox x:Name="pageViewSize">
<ComboBox.Items>
<ComboBoxItem IsSelected="True">5</ComboBoxItem>
<ComboBoxItem>10</ComboBoxItem>
<ComboBoxItem>20</ComboBoxItem>
<ComboBoxItem>30</ComboBoxItem>
<ComboBoxItem>50</ComboBoxItem>
<ComboBoxItem>100</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
Now i would like my DataPager's PageSize (which is the source to a DataGrid) be bound to this ComboBox's SelectedItem.Value (or is it SelectedValue?):
<DataPager PageSize="{Binding Path=SelectedItem.Value, ElementName=pageViewSize}" Source="{Binding PageView}"/>
This, unfortunately, is not working. The initial pagesize is not 10. And whenever i changed the selection in the ComboBox nothing happens to the displayed pagesize in the DataGrid.
What am i doing wrong?
Thanks
From DataPager.PageSize documentation:
The source typically implements the IPagedCollectionView interface. In this case, PageSize gets or sets the IPagedCollectionView.PageSize of the IPagedCollectionView.
If the source is a collection that implements IEnumerablebut not IPagedCollectionView, the DataPager ignores PageSize.
Maybe your data source doesn't properly support PageSize?
EDIT: I currently have the same issue as you I had the same issue as you, it was fixed by using #devdigital's answer.
I'm using data binding instead of element binding, on radio buttons + custom converter instead of combo, but it applies in the same way.
What I'm doing is data binding IsChecked to a value in my View Model, with a custom two-way converter checking whether value is equal to converter's parameter.
So here is an example from one of my RadioButtons:
IsChecked="{Binding MyBindedValue, Converter={StaticResource EqualStringConverter}, ConverterParameter=5, Mode=TwoWay}"
And your DataPager, modified:
<DataPager PageSize="{Binding MyBindedValue, Mode=TwoWay}" Source="{Binding PageView}"/>
Try setting the Mode to TwoWay.
PageSize="{Binding Path=SelectedItem.Value, Mode=TwoWay, ElementName=pageViewSize}"
So, my stack panel should be hidden or shown depending on the binding to one of the properties on DTO. Problem is that there is a slight delay, so it flashes before it disappears when the property is set to false.
Use the FallbackValue of the binding... It will use the FallbackValue if it can't bind, then when the property gets set, the visibility will change.
<StackPanel Visibility="{Binding ShowImageVisibility, FallbackValue=Collapsed}">
</StackPanel>
I'm trying to databind to a listbox like so:
<ListBox x:Name="MyListBox" Margin="0,0,0,65">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource MyConverter}}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The reason I am binding to the whole object and not a property is because my converter will need multiple properties of the object to build the string that it returns.
This works and my string is returned. But then when I change the ObservableCollection that this is based on the value doesn't change on the screen. If I bind to just a single property and change it, then the value does change.
What can I do differently? I can't bind to a single property since I need the entire object in the converter... And the ConverterParameter is already being used.
Remember, if you bind to the "main" property and the value of the main property itself isn't changed, the binding will have no reason to refresh itself. It has no clue that your converter is actually based off of a sub-property. What you can do is use a MultiBinding where you bind not only the "main" property, but also a specific sub-property. This gives your IMultiValueConverter implementation access to the main data object, but because you're also binding to the sub-property that's changing, will also be refreshed when that sub-property's value changes.
You can try using a MultiBinding which I believe updates whenever any of its Bindings are triggered. You can also use an IMultiValueConverter or just take advantage of the StringFormat of the binding.