Binding to ComboBox with converter in Silverlight - wpf

I have a numeric value that I wish to be converted to a more user-friendly string format when it's displayed. I already have an IValueConverter called FlightLevelConverter that I'm using to do this for a normal TextBlock UI item where it works fine.
I now wish to apply the converter to a ComboBox of altitude choices, but I can't get it to work.
This is the relevant part of the XAML I'm using for the ComboBox:
<UserControl.Resources>
<status:FlightLevelConverter x:Key="FlightLevelConverter"/>
</UserControl.Resources>
...
<ComboBox x:Name="AltitudeCombo" Grid.Row="0" Grid.Column="3" Width="100">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource FlightLevelConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
It displays the un-converted numeric values, not the nice string values. I get no errors and if I set a breakpoint in the converter it doesn't get hit, showing that the converter is never called.
I've spent all morning trawling the Internet in general and StackOverflow in particular to try to discover what I'm doing wrong, but haven't found out anything useful.
Why is my converter not being called? What am I doing wrong?

How do you add the items to the ComboBox?
You should set the ItemsSource property to a collection of numeric values, e.g.
List<double> values = new List<double>();
values.Add(2.1);
values.Add(3.2);
values.Add(4.3);
values.Add(5.4);
AltitudeCombo.ItemsSource = values;
If you add ComboBoxItems like this
AltitudeCombo.Items.Add(new ComboBoxItem() { Content = 1.4 });
the ItemTemplate and hence the binding with its converter won't be applied.

Here is a short working sample. You can compare code...
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:bys="clr-namespace:WpfApplication1"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<bys:MyList x:Key="lst"/>
<bys:MyConverter x:Key="myConverter"/>
</Grid.Resources>
<ComboBox ItemsSource="{StaticResource lst}" SelectedIndex="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource myConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox >
</Grid>
</Window>
C#:
public class MyList : List<int> {
public MyList() {
AddRange(new[] { 1, 2, 3, 4, 5, 6 });
}
}
public class MyConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return String.Format("<<{0}>>", value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
}

ComboBox.ItemTemplate is not applicable to the main part of the ComboBox if ComboBox.IsEditable == true. It works for dropdown list entries only. Try to set ComboBox.IsEditable == false. It might help.

Related

How can I binding an enum with IValueConverter?

Here is the enum:
public enum BarcodeType
{ AZTEC, CODABAR, CODE128, CODE93, CODE39, DATA_MATRIX, EAN13, EAN8, ITF, MAXICODE, PDF417, QRCODE, RSS14, RSSEXPANDED, UPCA, UPCE, UPC_EAN_EXTENSION }
And I bind the enum to the ComboBox like this:
<Page x:Class="KongGamLung.ToolProperty.BarCodeProperty"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:KongGamLung.ToolProperty"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:Model="clr-namespace:KongGamLung.Models"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="BarCodeProperty">
<Page.Resources>
<ObjectDataProvider x:Key="dataFromEnum" MethodName="GetValues"
ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="Model:BarcodeModel+BarcodeType"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<local:BarcodeTypeConverter x:Key="BarcodeTypeConverter"/>
</Page.Resources>
<ComboBox x:Name="BarcodeTypeCB" ItemsSource="{Binding Source={StaticResource dataFromEnum},Converter={StaticResource BarcodeTypeConverter}}">
</ComboBox>
</Page>
And here is code-behind:
public class BarcodeTypeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Enum.GetName(value.GetType(), value).ToString().Replace("_", " ");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
The code works well while without the code of the IValueConverter.
I use the IValueConverter for I want to replace the character of '_' in enum to ' ' to make it looks better.
I code the IValueConverter as what https://social.msdn.microsoft.com/Forums/vstudio/en-US/43db6b07-f886-4214-8076-5a5ec2360616/valueconverter-that-converts-an-enum-value-to-its-corresponding-string-value?forum=wpf said. But finally, it throws a System.ArgumentException error.
How can I solve it? Would you please help me? Thank you.
Don't use a converter on ItemsSource, it's changes the type of data that you're binding to. If you need to change the appearance of the enum then specify an ItemTemplate instead and use your converter there:
<ComboBox ItemsSource="{Binding Source={StaticResource dataFromEnum}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=., Converter={StaticResource BarcodeTypeConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Personally I'd bind to an intermediate view model class instead that contains both the enum and the text, that makes it much easier down the track to support things like localization (i.e. multiple languages at runtime).

WPF Combobox bind text

Maybe a simple task but I can't find the solution. I have a combobox connected to a database. Instead of displaying the content of ProductLookup I just want to display the words 'male' and 'female' on the popup menu.
Thank you
<ComboBox Height="23" Name="ComboBox2" Width="120" IsEditable="False"
ItemsSource="{Binding Source={StaticResource ProductLookup}}"
SelectedValue="{Binding Path=ProductID}"
SelectedValuePath="ProductID"
DisplayMemberPath="Name"/>
Write a Converter that takes one of your "Product" objects....looks at the gender related data inside it, or does the gender determining logic, and then returns the gender string, "male" or "female".
Then use it in your XAML to set the TextBlock:
<StackPanel Height="197" HorizontalAlignment="Left" Margin="300,6,0,0" Name="StackPanel5" VerticalAlignment="Top" Width="285"
DataContext="{Binding Source={StaticResource DetailViewPagos}}">
<StackPanel.Resources>
<local:ProductToGenderConverter x:Key="prodtogenderconv"/>
</StackPanel.Resources>
<ComboBox Height="23" Name="ComboBox2" Width="120" IsEditable="False"
ItemsSource="{Binding Source={StaticResource ProductLookup}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource prodtogenderconv}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
public class ProductToGenderConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
MyProduct prod = value as MyProduct;
if (prod is for a male) // Pseudocode for condition
return "male";
if (prod is for a female) // Pseudocode for condition
return "female";
return null or "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
}
Alternatively you could provide a ViewModel which wraps your Product object, which has a specific property that indicates the "genderness" of the Product...then create a collection of those objects for setting in your ComboBox...then you can use DisplayMemberPath to point to that property.
Please see this answer, it is a way to wrap your values for nice binding and display in WPF.
The Item class could be modified to support object for the Code property.

Change DataTemplate to use depending on condition

I have 3 user controls
Control 1
Control 2
Control 3
I have a stack panel that contains an ItemsControl
<UserControl.Resources>
<DataTemplate x:Key="Template1">
<my:UserControl1 Height="117"/>
</DataTemplate>
<DataTemplate x:Key="Template2">
<my:UserControl3 Height="117"/>
</DataTemplate>
<DataTemplate x:Key="Template3">
<my:UserControl3 Height="117"/>
</DataTemplate>
</UserControl.Resources>
<StackPanel Name="stackPanel3" Orientation="Vertical" VerticalAlignment="Bottom" Width="Auto">
<ItemsControl ItemsSource="{Binding BlocksForMonth.Blocks}" ItemTemplate="{StaticResource Template1}">
</ItemsControl>
</StackPanel>
BlocksForMonths.Blocks is a list of view models. The Blocks class has a property called ClipType. If the clipType is 1, I want to use Template1. If its 2 I want to use Template 2. If its 3 I want to use Template 3
These templates contain different user controls
How can I do this through binding?
I have considered 1 template with the 3 controls, but I dont know how to bind the visibility?
In this XAML I am binding to a list not a single item
Paul
I would put the 3 controls in the same template and use Visibility to display the correct one. What I would do is build an IValueConverter to convert the deciding value (your case it's ClipType) and compare that to the ConverterParameter. If they are equal, return Visibility.Visible, otherwise return Visibility.Collapsed.
<UserControl.Resources>
<my:ClipTypeToVisibilityConverter x:Key="converter"/>
<DataTemplate x:Key="Template">
<StackPanel>
<my:UserControl1 Height="117" Visibility={Binding ClipType, Converter={StaticResource converter}, ConverterParameter=1} />
<my:UserControl2 Height="117" Visibility={Binding ClipType, Converter={StaticResource converter}, ConverterParameter=2} />
<my:UserControl3 Height="117" Visibility={Binding ClipType, Converter={StaticResource converter}, ConverterParameter=3} />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<StackPanel Name="stackPanel3" Orientation="Vertical" VerticalAlignment="Bottom" Width="Auto">
<ItemsControl ItemsSource="{Binding BlocksForMonth.Blocks}" ItemTemplate="{StaticResource Template}">
</ItemsControl>
</StackPanel>
This example assumes the ClipType property is on each item view model in the list being displayed.
Here is a C# example converter.
public class ClipTypeToVisibilityConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var clipType = value.ToString();
if (clipType == (string)parameter))
return Visibility.Visible;
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Sorry, everything was air-code. But I think you get the idea.

Data Binding in Combobox

I binded a database table's primary key to the selectedIndex of a combobox. the problem occurs where the primary key starts from 1 but selectedIndex accepts from 0. I mean, when I want to see the item with ID=1 in database, since it's listed as first element in combobox with index 0, it displays the second element in the list, which is considered with ID=1 in the combobox. Can anyone help me on solving this problem?
Thanks in advance.
here's my combobox:
<ComboBox SelectedIndex="{Binding SC.User1.UserID, UpdateSourceTrigger=PropertyChanged }"
IsSynchronizedWithCurrentItem="True"
x:Name="proxyResponsibleUserCmb" ItemsSource="{Binding Users, Mode=OneTime}"
SelectedItem="{Binding SC.User1.FullName, ValidatesOnDataErrors=True,
UpdateSourceTrigger=PropertyChanged}"
Validation.ErrorTemplate="{x:Null}"
Height="23"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="118"
Margin="184,3,0,0"
Grid.Row="0"
Grid.Column="1"/>
What about using the ComboBox's SelectedValuePath and DisplayMemberPath, and setting your default item with SelectedValue instead of SelectedItem?
<ComboBox x:Name="proxyResponsibleUserCmb"
SelectedValuePath="{Binding UserID}"
DisplayMemberPath="{Binding FullName}"
SelectedValue="{Binding SC.User1.UserId, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Users, Mode=OneTime}" />
Does setting the property IsSynchronizedWithCurrentItem (in your XAML) to True help?
EDIT
Maybe this link will help:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/b4e84ea2-9597-4af1-8d3c-835b972e3d73
Quick workaround via a ValueConverter:
Create a ValueConverter in your codebehind:
// of course use your own namespace...
namespace MyNameSpace
{
public class IndexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if(!(value is int)) // Add the breakpoint here!!
throw new Exception();
int newindex = ((int)value - 1;
return newindex;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException("This method should never be called");
}
}
}
Then, make it known in your XAML:
//(declare a namespace in your window tag:)
xmlns:myNamespace="clr-namespace:MyNameSpace"
// add:
<Window.Resources>
<ResourceDictionary>
<myNamespace:IndexConverter x:Key="indexConverter" />
</ResourceDictionary>
</Window.Resources>
Then change your binding:
<ComboBox SelectedIndex="{Binding SC.User1.UserID, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource indexConverter}}" ... />
That should do the trick. At least you can debug it by inserting a breakpoint in the IndexConverter.

Show the validation error template on a different control in WPF

I have a UserControl that contains other controls and a TextBox. It has a Value property that is bound to the TextBox text and has ValidatesOnDataErrors set to True.
When a validation error occurs in the Value property binding, the error template (standard red border) is shown around the entire UserControl.
Is there a way to show it around the TextBox only?
I'd like to be able to use any error template so simply putting border around textbox and binding its color or something to Validation.HasError is not an option.
Here's my code:
<DataTemplate x:Key="TextFieldDataTemplate">
<c:TextField DisplayName="{Binding Name}" Value="{Binding Value, Mode=TwoWay, ValidatesOnDataErrors=True}"/>
</DataTemplate>
<controls:FieldBase x:Name="root">
<DockPanel DataContext="{Binding ElementName=root}">
<TextBlock Text="{Binding DisplayName}"/>
<TextBox x:Name="txtBox"
Text="{Binding Value, Mode=TwoWay, ValidatesOnDataErrors=True}"
IsReadOnly="{Binding IsReadOnly}"/>
</DockPanel>
UserControl (FieldBase) is than bound to ModelView which performs validation.
to accomplish this task I've used this solution. It uses converter, that "hides" border by converting (Validation.Errors).CurrentItem to Thickness.
<Grid>
<Grid.Resources>
<data:ValidationBorderConverter
x:Key="ValidationBorderConverter" />
</Grid.Resources>
<Border
BorderBrush="#ff0000"
BorderThickness="{Binding
ElementName=myControl,
Path=(Validation.Errors).CurrentItem,
onverter={StaticResource ValidationBorderConverter}}">
<TextBox
ToolTip="{Binding
ElementName=myControl,
Path=(Validation.Errors).CurrentItem.ErrorContent}" />
</Border>
</Grid>
ValidationBorderConverter class is pretty simple:
[ValueConversion(typeof(object), typeof(ValidationError))]
public sealed class ValidationBorderConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return (value == null) ? new Thickness(0) : new Thickness(1);
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

Resources