WPF Radio Button Fires Converter in strange order - wpf

I am writing a simple program using the MVVM Model on WPF. Basicly when the user clicks a radio button in a group of radio buttons, it will update a property in the View Model with the new Account number. The problem is, when I click a different button the converter is called for the new button IsChecked Binding, and then after that it runs the converter for the previous button IsChecked binding(for losing its checked status).
This is causing a problem, since the new button is updating the value of the property with the correct account number, and then when the old button calls the converter, it gets converted back to the old value. I have hacked it to work by adding a static variable to the class, and if the IsChecked property is false, just return the value in the static variable. Does anyone have a better solution for Short Circuting the Converter Call on the box that loses its checked status. Code is below:
Converter:
class RadioToAccountConverter : IValueConverter
{
static string myValue; //HACK TO MAKE IT WORK
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return parameter.ToString();
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool)value)
{
myValue = parameter.ToString(); // Hack to make it work
return parameter.ToString();
}
return myValue; // Hack to make it work
}
}
XAML:
<RadioButton Foreground="HotPink"
Grid.Column="0"
Content="6087721"
Tag="6087721"
IsChecked="{Binding Account, Converter={StaticResource Radio2Value}, Mode=OneWayToSource, ConverterParameter=6087721}">
</RadioButton>
<RadioButton Foreground="HotPink"
Grid.Column="1"
Content="BFSC120"
IsChecked="{Binding Account, Converter={StaticResource Radio2Value}, Mode=OneWayToSource, ConverterParameter='BFSC120'}">
</RadioButton>
<RadioButton Foreground="HotPink"
Grid.Column="2"
Content="BFSC121"
IsChecked="{Binding Account, Converter={StaticResource Radio2Value}, Mode=OneWayToSource, ConverterParameter=BFSC121}">
</RadioButton>
<RadioButton Foreground="HotPink"
Grid.Column="3"
Content="BFSC206"
IsChecked="{Binding Account, Converter={StaticResource Radio2Value}, Mode=OneWayToSource, ConverterParameter=BFSC206}">
</RadioButton>
Property:
public const string AccountPropertyName = "Account";
private string _account;
/// <summary>
/// Sets and gets the Account property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string Account
{
get
{
return _account;
}
set
{
if (_account == value)
{
return;
}
RaisePropertyChanging(AccountPropertyName);
_account = value;
RaisePropertyChanged(AccountPropertyName);
}
}
Any Help Is Greatly Appreciated.

Based on what I understand, you want to give users the ability to select from a list of account numbers. You're choice of presentation (view) is a group of radio buttons.
If that is true, the key part is this: you want to give users the ability to select from a list of account numbers. This means that the control you should use is a ListBox, since users should select one of the appropriate values. Now, since you are looking to use radio buttons visually, you simply have to supply an alternative ItemsSource.ItemContainerStyle.
XAML:
<ListBox ItemsSource="{Binding AccountNumbers, Mode=OneWay">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<RadioButton Content="{Binding}" IsChecked="{Binding IsSelected, RelativeSource={x:Static RelativeSource.TemplatedParent}}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Note that you'll need to add another property on your ViewModel (I named it AccountNumbers). For example:
public IReadOnlyCollection<string> AccountNumbers { ... }
Of course, the underlying collection can be a observable if you need it to be, but that's really up to you.

If you define a GroupName on each RadioButton, WPF will manage the IsChecked states for you.
You could bind the state with a {Binding SomeProperty, Mode=OneWayToSource} if you want the ViewModel to be aware of state.
One way to approach this would be to bind each RadioButton's IsChecked property to the whole ViewModel, just bind it to something like
IsChecked="{Binding WholeViewModel, Mode=OneWayToSource, Converter={StaticResource MyRadioButtonConverter}, ConverterParameter=SomethingReallyUnique}"
...where the public property WholeViewModel is a property that does a return this; in the getter. This would let you have access to the ViewModel and enough information to query the ViewModel to see if the radiobutton should be checked or not. But, only do this if the GroupName DependencyProperty doesn't somehow give you what you want.
To process the clicking on the buttons, then, to actually change the ViewModel state, you'd implement an ICommand in your ViewModel and bind the Command property of the RadioButton to {Binding ClickedCommand} and define a CommandParameter with any string you want. This approach will guarantee a one-way relationship to the IsChecked state, preventing the thing you're describing, I think.
I'll work up a code sample if you think you'd like one.

Related

How to display a different WPF DataTemplate for multiple object types

The following blog post demonstrates how to dynamically display a different DataTemplate depending on which object is displayed in a ListBox:
http://www.thejoyofcode.com/different_datatemplates_for_different_types.aspx
Although this is useful, my particular situation is a bit more challenging.
I have a collection of objects in my main viewmodel:
public IEnumerableCollection<IGenericObject> CurrentObjects
I currently display them in a ComboBox using XAML as follows:
<ComboBox ItemsSource="{Binding CurrentObjects}"
SelectedItem="{Binding SelectedObject,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged }"
DisplayMemberPath="Name"
SelectedIndex="0"/>
I would now like to have a separate panel below the ComboBox which displays properties for each object. However, each object has different properties depending on its concrete type.
For example, a AObject would not only support IGenericObject but IAObject as well, so I want to always display properties that are common to all objects and then dynamically display those that are specific whatever object is currently selected.
I don't know how to set up the bindings and obtain the property values for each specific object type given the collection I have makes use of the common IGenericObject interface.
How can I achieve this?
which displays properties for each object.
Keep in mind that binding at the end of the process is just reflection. One can place property names and if the binding fails, nothing is shown.
However, each object has different properties depending on its concrete type.
Ultimately we are using a converter to hide labels and textblocks depending on the target type contained as the selected item in the combobox.
Example
This example is a person and an employee which share the same attributes such as First, Last and Phone, but the employee has an EmployeeId unlike the person. We want to show the first and last name for both, but if its an employee also show its Id. Also swap out the headers stating if it is an employee or a person.
Last names in combo, two objects, Smith is a person and Man is an employee. So when we have a person the screen shows this:
and when its an employee show this with its ID for the last info:
Xaml
<Page x:Class="WPFStack.Views.BindingCoverterPage"
...
xmlns:local="clr-namespace:WPFStack.Views"
xmlns:model="clr-namespace:WPFStack.Model"
xmlns:converter="clr-namespace:WPFStack.Converters"
.>
<Page.Resources>
<model:People x:Key="people">
<model:Person First="Joe"
Last="Smith"
Phone="303-555-5555" />
</model:People>
<model:Employees x:Key="employeePeople">
<model:Employee First="Omega"
Last="Man"
Phone="303-867-5309"
EmployeeId="90125" />
</model:Employees>
<converter:EmployeeVisiblity x:Key="isEmployeeVisibility"/>
<Style x:Key="LabelStyle" TargetType="Label" >
<Setter Property="Margin" Value="-20,0,0,0"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</Page.Resources>
<StackPanel Margin="10"
HorizontalAlignment="Left">
<ComboBox Name="mySelectionCombo"
SelectedItem="{Binding SelectedObject,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged }"
DisplayMemberPath="Last"
SelectedIndex="0"
Width="200">
<ComboBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource people}}" />
<CollectionContainer Collection="{Binding Source={StaticResource employeePeople}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
<StackPanel Margin="10">
<Label Content="Employee Stats:"
Style="{StaticResource LabelStyle}"
Visibility="{Binding SelectedItem,
ElementName=mySelectionCombo,
Converter={StaticResource isEmployeeVisibility}}" />
<Label Content="Person Stats:"
Style="{StaticResource LabelStyle}"
Visibility="{Binding SelectedItem,
ElementName=mySelectionCombo,
ConverterParameter=Reverse,
Converter={StaticResource isEmployeeVisibility}}" />
<TextBlock Text="{Binding SelectedItem.First, ElementName=mySelectionCombo}" />
<TextBlock Text="{Binding SelectedItem.Last, ElementName=mySelectionCombo}" />
<TextBlock Text="{Binding SelectedItem.EmployeeId, ElementName=mySelectionCombo}"
Visibility="{Binding SelectedItem,
ElementName=mySelectionCombo,
Converter={StaticResource isEmployeeVisibility}}" />
</StackPanel>
</StackPanel>
</Page>
Converter
namespace WPFStack.Converters
{
public class EmployeeVisiblity : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var isVisible = Visibility.Collapsed;
if (value != null)
if (value is Employee)
{
if (parameter == null) // Does not say string "Reverse"
isVisible = Visibility.Visible;
}
else // Value is a person
{
if (parameter != null) // Does say string "Reverse"
isVisible = Visibility.Visible;
}
return isVisible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
}
Usage
Note that what the example is primarily doing is simply binding to a similar (but not the same type of things as your data) for it is process and not the data one needs to focus on for the answer to the question.
Even though I am using a Composite Collection to hold set of data such as yours via ItemsSource, I end up (like you) with a list of different instance objects, just like your data.
So....
Focus on that point on and see how the converter works to determine what to make visible and what not depending on what the combobox has selected.
Here are the following steps which you will need to implement and understand to make it work in your code:
Make a converter like the one in your project. Note the namespace.
Make a xaml import reference and name it converter. My namespace was WPFStack.Converters so bringing it into Xaml made mine xmlns:converter="clr-namespace:WPFStack.Converters".
Create a static instance in Xaml of the converter you created by specifying it in the page resources <converter:EmployeeVisiblity x:Key="isEmployeeVisibility"/>.
Any visual control on the screen you need to hide, on its Visbility property will bind to the current selected item of the combo (which you may need to provide a name for the binding) and also call your converter to determine if it is shown or not such as this xaml code:
<TextBlock Text="{Binding SelectedItem.EmployeeId, ElementName=mySelectionCombo}"
Visibility="{Binding SelectedItem,
ElementName=mySelectionCombo,
Converter={StaticResource isEmployeeVisibility}}" />
That is the takeaway you need to have from this response.

WPF tab control and MVVM selection

I have a TabControl in an MVVM WPF application. It is defined as follows.
<TabControl Style="{StaticResource PortfolioSelectionTabControl}" SelectedItem="{Binding SelectedParameterTab}" >
<TabItem Header="Trades" Style="{StaticResource PortfolioSelectionTabItem}">
<ContentControl Margin="0,10,0,5" Name="NSDetailTradeRegion" cal:RegionManager.RegionName="NSDetailTradeRegion" />
</TabItem>
<TabItem Header="Ccy Rates" Style="{StaticResource PortfolioSelectionTabItem}">
<ContentControl Margin="0,10,0,5" Name="NSDetailCcyRegion" cal:RegionManager.RegionName="NSDetailCcyRegion" />
</TabItem>
<TabItem Header="Correlations / Shocks" Style="{StaticResource PortfolioSelectionTabItem}">
<ContentControl Name="NSDetailCorrelationRegion" cal:RegionManager.RegionName="NSDetailCorrelationRegion" />
</TabItem>
<TabItem Header="Facility Overrides" Style="{StaticResource PortfolioSelectionTabItem}" IsEnabled="False">
<ContentControl Name="NSDetailFacilityOverrides" cal:RegionManager.RegionName="NSDetailFacilityOverrides" />
</TabItem>
</TabControl>
So each tab item content has its own view associated with it. Each of those views has the MEF [Export] attribute and is associated with the relevant region through view discovery, so the above code is all I need to have the tab control load and switch between them. They all reference the same shared ViewModel object behind them and so all interact seamlessly.
My problem is that when the user navigates to the parent window, I want the tab control to default to the second tab item. That is easy enough to do when the window is first loaded, by specifying in XAML IsSelected="True" in TabItem number 2. It is less easy to do when the user navigates away from the screen and then comes back to it.
I thought about having a SelectedItem={Binding SelectedTabItem} property on the tab control, so I could programmatically set the selected tab in the ViewModel, but the problem is I have no knowledge of the TabItem objects in the ViewModel as they are declared above in the XAML only, so I have no TabItem object to pass to the setter property.
One idea I had was to make the child Views (that form the content of each of the tab items above) have a style on the UserControl level of their XAML, something along the following.
<Style TargetType={x:Type UserControl}>
<Style.Triggers>
<DataTrigger Property="IsSelected" Value="True">
<Setter Property="{ElementName={FindAncestor, Parent, typeof(TabItem)}, Path=IsSelected", Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
I know the findancestor bit isn't correct; I've just put it there to specify my intent, but I am not sure of the exact syntax. Basically for each UserControl to have a trigger that listens to a property on the ViewModel (not sure how I would distinguish each different UserControl as obviously they can't all listen to the same property or they would all select simultaneously when the property is set to True, but having a property for each usercontrol seems ugly) and then finds its parent TabItem container and sets the IsSelected value to true.
Am I on the right track with a solution here? Is it possible to do what I am pondering? Is there a tidier solution?
If you look at the TabControl Class page on MSDN, you'll find a property called SelectedIndex which is an int. Therefore, simply add an int property into your view model and Bind it to the TabControl.SelectedIndex property and then you can select whichever tab you like at any time from the view model:
<TabControl SelectedIndex="{Binding SelectedIndex}">
...
</TabControl>
UPDATE >>>
Setting a 'startup' tab is even easier using this method:
In view model:
private int selectedIndex = 2; // Set the field to whichever tab you want to start on
public int SelectedIndex { get; set; } // Implement INotifyPropertyChanged here
Just FYI,
I gone through the same issue where I add tabs dynamically using ObservableCollection source but last added Tab do not get selected.
I have done same changes what Sheridan said to select Tab as per SelectedIndex. Now last added Tab gets selected but it was not getting focused.
So to focus the Tab we have to add set Binding IsAsync property True.
<TabControl ItemsSource="{Binding Workspaces}" Margin="5" SelectedIndex="{Binding TabIndex, Mode=OneWay,UpdateSourceTrigger=PropertyChanged, IsAsync=True}">
The below code sample will create a dynamic tab using MVVM.
XAML
<TabControl Margin="20" x:Name="tabCategory"
ItemsSource="{Binding tabCategory}"
SelectedItem="{Binding SelectedCategory}">
<TabControl.ItemTemplate>
<DataTemplate>
<HeaderedContentControl Header="{Binding TabHeader}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ContentControl Content="{Binding TabContent}" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
Modal Class
TabCategoryItem represents each tab item. On two properties, TabHeader will display a tab caption and TabContent contains the content/control to fill in each tab.
Public Class TabCategoryItem
Public Property TabHeader As String
Public Property TabContent As UIElement
End Class
VM Class
Public Class vmClass
Public Property tabCategory As ObjectModel.ObservableCollection(Of TabCategoryItem)
Public Property SelectedCategory As TabCategoryItem
End Class
The below code will fill and bind the content. I am creating two tabs, tab1 and tab2. Both tabs will contain text boxes. You can use any UIelement instead of text boxes.
Dim vm As New vmClass
vm.tabCategory = New ObjectModel.ObservableCollection(Of TabCategoryItem)
'VM.tabCategory colection will create all tabs
vm.tabCategory.Add(New TabCategoryItem() With {.TabHeader = "Tab1", .TabContent = new TextBlock().Text = "My first Tab control1"})
vm.tabCategory.Add(New TabCategoryItem() With {.TabHeader = "Tab2", .TabContent = new TextBlock().Text = "My first Tab control2"})
mywindow.DataContent = vm
The accepted answer is not working with DependencyObject on your ViewModel .
I'm using MVVM with DependencyObject and Just setting the TabControl didn't work for me.The problem I had was the the property was not getting update on the View when I was setting the tab selectedIndex from the ViewModel.
I did set the Mode to be two ways but nothing was working.
<TabControl SelectedIndex="{Binding SelectedTab,Mode=TwoWay}" >
...
</TabControl>
The ViewModel property "SelectedTab" was getting updated all the time when I navigated between tabs. This was confirming my binding was working properly. Each time I would navigate the tabs both the Get and Set would get called in my ViewModel. But if I try to set the SelectedIndex in the ViewModel it would not update the view.
ie: SelectedTab=0 or SelectedTab=1 etc...
When doing the set from the ViewModel the SelectedTab 'set' method would be called, but the view would never do the 'get'.
All I could find online was example using INotifyPropertyChanged but I do not wish to use that with my ViewModel.
I found the solutions in this page: http://blog.lexique-du-net.com/index.php?post/2010/02/24/DependencyProperties-or-INotifyPropertyChanged
With DependencyObject, you need to register the DependencyProperties. Not for all properties but I guess for a tabcontrol property you need to.
Below my code:
view.xaml
//Not sure below if I need to mention the TwoWay mode
<TabControl SelectedIndex="{Binding SelectedTab,Mode=TwoWay}" >
...
</TabControl>
ViewModel.cs
public class ViewModel : DependencyObject
{
public static readonly DependencyProperty SelectedTabDP = DependencyProperty.Register("SelectedTab", typeof(int), typeof(ViewModel));
public int SelectedTab
{
get { return (int)GetValue(SelectedTabDP); }
set { SetValue(SelectedTabDP, value); }
}
}
Basically all I had to do was to actually register the dependency property (DependencyProperty) as you can see above.
What made this hard to figure out was that I have a bunch of other Properties on that view and I didn't need to register them like that to make it work two ways. For some reason on the TabControl I had to register the property like I did above.
Hope this help someone else.
Turns out my problem were because my components have names:
x:Name="xxxxxxxx"
Giving names to components at the same time of biding them with DependencyObject seems to be the main cause of all my issues.
In order to improve semantic of my viewmodel and to not work with an int when using code to check for the selected tab, I made some additions to the accepted answer so to use an Enum instead of an int.
These are the steps:
Define an Enum representing the different tabs:
public enum RulesVisibilityMode {
Active,
History
}
Expose the SelectedTab as a property using the enum instead of the int:
public RulesVisibilityMode SelectedTab { get; set; }
Create a converter to convert from an int to your enum (I don't need the ConvertBack because I never select the active tab from the code, but you can add it too):
internal class RulesVisibilityModeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException("Conversion from visibility mode to selected index has not been implemented");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
int selectedTabIndex;
if (int.TryParse(value.ToString(), out selectedTabIndex))
{
return (RulesVisibilityMode)selectedTabIndex;
}
return null;
}
}
Bind the tabcontrol to the SelectedTab property through the converter:
<TabControl SelectedIndex="{Binding SelectedTab, Mode=OneWayToSource, Converter={StaticResource RulesVisibilityModeConverter}}" ...
Now every time you need to check for the selected tab in the code you deal with a readable enum:
if (this.SelectedTab != RulesVisibilityMode.Active) ...

DependencyProperty binding not happening on initial load

I'm trying to do something simple -- make a DependencyProperty and then bind to it. However, the getter doesn't appear to fire when I start up the app. (I'm guessing the solution will make me smack my head and go "Doh!", but still. :) )
Any ideas?
Code-behind code:
public static readonly DependencyProperty PriorityProperty =
DependencyProperty.Register("Priority",
typeof (Priority), typeof (PriorityControl), null);
public Priority Priority
{
get { return (Priority)GetValue(PriorityProperty); }
set { SetValue(PriorityProperty, value); }
}
Control XAML:
<ListBox Background="Transparent"
BorderThickness="0"
ItemsSource="{Binding Path=Priorities}"
Name="PriorityList"
SelectedItem="{Binding Path=Priority, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="16" Width="16">
<Border BorderBrush="Black"
BorderThickness="2"
CornerRadius="3"
Visibility="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected, Converter={StaticResource boolToVisibilityConverter}}" />
<Border CornerRadius="3" Height="12" Width="12">
<Border.Background>
<SolidColorBrush Color="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=Content, Converter={StaticResource priorityToColorConverter}}" />
</Border.Background>
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Binding statement:
<UI:PriorityControl Grid.Column="8"
Priority="{Binding Path=Priority}"
VerticalAlignment="Center"/>
Some other notes:
Binding is in a UserControl
UserControl contains the PriorityControl
PriorityControl contains the DependencyProperty
I've checked that the data the UserControl is getting the appropriate data -- every other binding works.
If I change the selection on the PriorityControl via the mouse, everything fires as appropriate. It's just that initial setting of the value that isn't working.
Priority is an enum.
EDIT: I've tried two additional things. First, I made the binding to the value two-way, but that didn't work. Then, I added a property changed callback to the dependency property and made it call OnPropertyChanged, like so:
public static readonly DependencyProperty PriorityProperty =
DependencyProperty.Register("Priority", typeof (Priority), typeof (PriorityControl), new PropertyMetadata(HandleValueChanged));
private static void HandleValueChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var npc = dependencyObject as PriorityControl;
npc.OnPropertyChanged("Priority");
}
That didn't work either. I even tried overriding ApplyTemplate on the control to force a property change notification, but the value is never getting set on initial load. I can change values and see everything fire just fine, but it's that first time that nothing is coming through.
The getter and setter for a DependencyProperty are not called by anyone except you!
They are simply helpers generated for your benefit (to save you lots of casting and calls to GetValue/SetValue).
All work in DPs (animation, binding etc) happens directly via calls to SetValue and GetValue.
If you need to catch a change in value, provide the change callback in the Register call.
Note: Attached properties (i.e. using RegisterAttached instead of Register) actually do use the setter, while parsing the XAML, but that is another story as normal DPs do not.
Update:
You have not set a default value in your Register, so it defaults to 0 (which is your enum value "Normal"). If you set that as the first value in your binding there is not change so it won't trigger.
One Solution: Add a "none" enum as your first 0 value, so that "Normal" (value 1) will cause a change. Alternatively set a default value in the Register that is not "Normal".
I've noticed that when doing dependency properties if the value is already been set during the definition of the property it won't act correctly. You have said that there were no problems when setting the value within the application, so I'm going to assume there is no plumbing problems.
Consider the example below:
XAML
<UI:PriorityControl Priority="{Binding Path=Priority, Mode=TwoWay}"/>
Code
public enum Priority
{
Normal,
Low,
High
}
public class PriorityControl : UserControl
{
public static readonly DependencyProperty PriorityProperty =
DependencyProperty.Register("Priority", typeof(Priority), typeof(PriorityControl), new PropertyMetadata(DependencyProperty.UnsetValue, OnPriorityPropertyChanged);
public Priority Priority
{
get { return (Priority)this.GetValue(PriorityProperty); }
set { this.SetValue(PriorityProperty, value); }
}
private static void OnPriorityPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
PriorityControl control = sender as PriorityControl;
if (control != null)
{
// Do what you wanted to here.
}
}
}
If you're using an enum, and not specifying a default value (which you appear not to be by not providing a PropertyMetadata object to the dependency property on creation) the default value would be that of the enum's default value. In my above example that would be "Normal". Now if you tried to set the value to Normal on creating the control, the callback would not be called because the value already equals that value.
In order to allow this to work correctly, you must pass the DependencyProperty.UnsetValue to the PropertyMetadata object as I have done when I defined the property. That will allow the OnPriorityPropertyChanged method to be called when the value is being set initially.
Hope that helps fix it!
Not 100% sure, but it might be as simple as changing the Priority binding to be TwoWay.
<UI:PriorityControl Grid.Column="8"
Priority="{Binding Path=Priority, Mode=TwoWay}"
VerticalAlignment="Center"/>

WPF Combobox is changing source via SelectedValue when ItemsSource is loaded

I have this combobox in my WPF window.
<ComboBox DisplayMemberPath="Description" SelectedValuePath="ID" ItemsSource="{Binding Source={StaticResource CvsPrinters}}" SelectedValue="{Binding CheckPrinterID}" />
My problem is that when loading the window, the SelectedValue binding is causing my source data to change to the first item in the ItemsSource, instead of setting the Combobox's SelectedValue to the appropriate item in the ItemsSource.
The CheckPrinterID is from a listview selection datacontext, and this problem only occurs to the item initially selected in that listview on load. When I select another item in the listbox, the combobox correctly selects the proper item and all is fine, but unfortunately my initial item has been updated and is now incorrect.
I guess you are trying to synchronize ListView and ComboBox through a common property. Try setting IsSynchronizedWithCurrentItem to True in ListView and make sure SelectedItem or SelectedIndex for ListView is set during load.
Try re-arranging ItemsSource before DisplayMemberPath.
If you have some flexibility in the DataContext object you could try changing the selected CheckPrinter property to be of the data object type instead of the ID and switch to using SelectedItem instead of SelectedValue (for some reason SelectedValue behaves differently, especially at initial load) and then extract the ID from that value in code.
If you can't use the CheckPrinter objects in your DataContext object for whatever reason, you could also go the opposite direction on the UI side by using a list of IDs as the ItemsSource, and again using SelectedItem. To get the list to show what you want in the ComboBoxItems you would then need to use an IValueConverter to pull out Description values based on IDs:
<ComboBox ItemsSource="{Binding Source={StaticResource CvsPrinterIds}}" SelectedItem="{Binding CheckPrinterID}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock >
<TextBlock.Text>
<Binding>
<Binding.Converter>
<local:MyDescriptionLookupConverter Printers="{StaticResource CvsPrinters}"/>
</Binding.Converter>
</Binding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
and a simple converter to do the ID-Description lookup (add some null and cast checks):
public class MyDescriptionLookupConverter : IValueConverter
{
public IEnumerable<Printer> Printers { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Printers.First(p => p.Id == (int)value).Description;
}
...
}

WPF Databinding CheckBox.IsChecked

How would I bind the IsChecked member of a CheckBox to a member variable in my form?
(I realize I can access it directly, but I am trying to learn about databinding and WPF)
Below is my failed attempt to get this working.
XAML:
<Window x:Class="MyProject.Form1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Title" Height="386" Width="563" WindowStyle="SingleBorderWindow">
<Grid>
<CheckBox Name="checkBoxShowPending"
TabIndex="2" Margin="0,12,30,0"
Checked="checkBoxShowPending_CheckedChanged"
Height="17" Width="92"
VerticalAlignment="Top" HorizontalAlignment="Right"
Content="Show Pending" IsChecked="{Binding ShowPending}">
</CheckBox>
</Grid>
</Window>
Code:
namespace MyProject
{
public partial class Form1 : Window
{
private ListViewColumnSorter lvwColumnSorter;
public bool? ShowPending
{
get { return this.showPending; }
set { this.showPending = value; }
}
private bool showPending = false;
private void checkBoxShowPending_CheckedChanged(object sender, EventArgs e)
{
//checking showPending.Value here. It's always false
}
}
}
<Window ... Name="MyWindow">
<Grid>
<CheckBox ... IsChecked="{Binding ElementName=MyWindow, Path=ShowPending}"/>
</Grid>
</Window>
Note i added a name to <Window>, and changed the binding in your CheckBox. You will need to implement ShowPending as a DependencyProperty as well if you want it to be able to update when changed.
Addendum to #Will's answer: this is what your DependencyProperty might look like (created using Dr. WPF's snippets):
#region ShowPending
/// <summary>
/// ShowPending Dependency Property
/// </summary>
public static readonly DependencyProperty ShowPendingProperty =
DependencyProperty.Register("ShowPending", typeof(bool), typeof(MainViewModel),
new FrameworkPropertyMetadata((bool)false));
/// <summary>
/// Gets or sets the ShowPending property. This dependency property
/// indicates ....
/// </summary>
public bool ShowPending
{
get { return (bool)GetValue(ShowPendingProperty); }
set { SetValue(ShowPendingProperty, value); }
}
#endregion
You must make your binding mode as TwoWay :
<Checkbox IsChecked="{Binding Path=ShowPending, Mode=TwoWay}"/>
If you have only one control that you want to bind to a property of your code-behind, then you can specify this as the source in your binding via a RelativeSource like this:
<CheckBox ...
IsChecked="{Binding ShowPending, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
That could be the end of the answer. But more generally you will have multiple controls and wish to bind them to various properties on your class. In this case it is neater and more convenient to make use of the fact that the DataContext property (which is the default source object for data binding) is inherited down through the control hierarchy, so setting it at the top level will make it available to all the child controls.
There is no default value for DataContext, but there are at least two ways you can set the DataContext property of your Window element to point at itself:
By setting DataContext = this in the code-behind constructor. This is very simple, but some might argue that it's not clear in the XAML where the DataContext is pointing.
By setting the DataContext in XAML using DataBinding
The simplest and, I think, most elegant way to set the DataContext at the Window/UserControl level in XAML is very straight forward; just add DataContext="{Binding RelativeSource={RelativeSource Self}}" to your Window element. RelativeSource Self just means "bind directly to the object", which in this case is the Window object. The lack of a Path property results in the default Path, which is the source object itself (i.e. the Window).
<Window x:Class="MyProject.Form1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<CheckBox ...
IsChecked="{Binding ShowPending}">
</CheckBox>
</Grid>
</Window>
Once you have done this, the DataContext property for all child controls will be the Window class, so data binding to properties in your code-behind will be natural.
If for some reason you don't want to set the DataContext on the Window, but wish to set it lower down the control hierarchy, then you can do so by using the FindAncestor mechanism. E.g. if you want to set it on the Grid element and all children of the Grid:
<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
<CheckBox ...
IsChecked="{Binding ShowPending}">
</CheckBox>
</Grid>
It's probably worth noting at this point that what we have achieved so far is the ability to bind a UI Control to a property of your code-behind class, and for that code-behind property to be kept up-to-date with changes to the UI element. So if the user checks the CheckBox, the ShowPending property will be updated.
But quite often you also want the reverse to be true; a change to the source property should be reflected in a corresponding change to the UI Control. You can see this by adding another CheckBox control to your window, bound to the same ShowPending property. When you click one checkbox, you would probably hope or expect the other Checkbox to be synchronized, but it won't happen. To achieve this your code-behind class should either (a) implement INotifyPropertyChanged, (b) add a ShowPendingChanged event or (c) make ShowPending a Dependency Property. Of the 3, I suggest implementing INotifyPropertryChanged on your code-behind is the most common mechanism.

Resources