I have a WPF textbox on a form to allow input of a URI.
I tried to do this using a data converter. The problem is that when the textbox binding updates and the textbox doesn't contain a valid URI,
The data converter returns a null ;
which sets my model property to null ;
which results in a property changed event firing ;
which sets the text box value to the empty string, wiping out the users invalid input
I'm a WPF novice, and I'm at a loss to find a simple pattern using a data converter that doesn't result in this behaviour. I'm thinking there must be a standard pattern to use, that I'd know about if I was an experienced WPF programmer.
Looking at the examples included with Prism 4, there seems to be two different approaches used. I dislike them both.
The first approach is to throw an exception when my model property is set null, which is caught and shown as a validation error. The problem is that I want the property to be able to be set to null - each time you open the form, the fields are set to their previous values. If the application has never been ran before, the URI will be set to null - this shouldn't throw an exception. Also, the use of exceptions for validation is ugly.
The second approach is when the property is set to null, set the validation state of the model to include the property invalidity, but don't actually update the property. Which I think is awful. It results in the model being internally inconsistent, claiming that the DCSUri is invalid, but containing the previous valid value of DCSUri.
The approach I'm using to avoid these issues is to have a string DCSUri in my ViewModel, which only updates the Uri typed DCSUri property of my Model if it is a valid URI. But I'd prefer an approach which allows use of a converter and binding my textbox directly to my model.
My converter code:
/// <summary>
/// Converter from Uri to a string and vice versa.
/// </summary>
public class StringToUriConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
Uri uri = null;
string stringValue = value as string;
if (stringValue != null)
Uri.TryCreate(stringValue, UriKind.Absolute, out uri);
return uri;
}
}
The XAML for the textbox:
<TextBox Grid.Row="1" Grid.Column="1" Name="DCSUriTextBox"
Text="{Binding Path=DCSLoadSettings.DCSUri, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True, Converter={StaticResource StringToUriConverter} }"
HorizontalAlignment="Stretch" Height="Auto" VerticalAlignment="Center" Margin="5,0,20,0" IsReadOnly="{Binding Path=IsNotReady}" Grid.ColumnSpan="2" />
And the code for the DCSUri property within my model:
/// <summary>
/// The Uri of the DCS instance being provided configuration
/// </summary>
public Uri DCSUri
{
get
{
return mDCSUri;
}
set
{
if (!Equals(value, mDCSUri))
{
mDCSUri = value;
this["DCSUri"] = value == null
? "Must provide a Uri for the DCS instance being provided configuration"
: string.Empty;
RaisePropertyChanged(() => DCSUri);
}
}
}
You should use ValidationRules for Validation, and name your converters the right way around; i would approach it like this (assuming that you want to be able to set the Uri to null):
public class UriToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Uri input = value as Uri;
return input == null ?
String.Empty : input.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string input = value as string;
return String.IsNullOrEmpty(input) ?
null : new Uri(input, UriKind.Absolute);
}
}
public class UriValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string input = value as string;
if (String.IsNullOrEmpty(input)) // Valid input, converts to null.
{
return new ValidationResult(true, null);
}
Uri outUri;
if (Uri.TryCreate(input, UriKind.Absolute, out outUri))
{
return new ValidationResult(true, null);
}
else
{
return new ValidationResult(false, "String is not a valid URI");
}
}
}
Then use it like this (or by defining the converter and rule as a resource somewhere):
<TextBox MinWidth="100">
<TextBox.Text>
<Binding Path="Uri">
<Binding.ValidationRules>
<vr:UriValidationRule />
</Binding.ValidationRules>
<Binding.Converter>
<vc:UriToStringConverter/>
</Binding.Converter>
</Binding>
</TextBox.Text>
</TextBox>
If the input text does not pass validation the Converter will not be called, that is why i have no TryCreate or anything like that in there.
There is a decent article about input validation on CodeProject which you might find to be helpful.
To test the value for null you could use another converter and a helper TextBlock:
public class NullToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value == null ?
"NULL" : value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
<TextBlock>
<TextBlock.Text>
<Binding Path="Uri">
<Binding.Converter>
<vc:NullToStringConverter/>
</Binding.Converter>
</Binding>
</TextBlock.Text>
</TextBlock>
Related
I have a enum to string converter
public class EnumToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
MailSettingsStateEnum enumValue = (MailSettingsStateEnum)value;
// extension method on the enum, to return a string based on enum.
return enumValue.Description();
}
// ConvertBack not relevant here.
}
I am using this in wpf xaml easily as follows to set the Content property of a label.
<Label Content="{Binding MailSettingState, Converter={StaticResource
EnumConverterString}}"
BorderBrush="{Binding MailSettingState, Converter={StaticResource
EnumConverterBorderBrush}}" />
Now as you can see, I have another property BorderBrush. I also have to set this based on the same enum. And so I had to write another converter EnumConverterBorderBrush
So is there a way by which I have only one converter, and it return an object which has two properties and i can use these properties in the xaml? I can create the converter, its easy, but I dont know how to use it in xaml. Say the converter returned an object and has tow property called MessageString(of type string), and another BorderBrush of the type Brush, how do I use it the xaml?
You can switch the output based on the targetType you receive in your converter.
So you could do something like this:
public class EnumToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
var enumValue = (MailSettingsStateEnum)value;
switch(targetType)
{
case typeof(string)
return enumValue.Description();
case typeof(Brush)
return enumValue.GetBrush();
default:
throw new NotSupportedException("Type not supported")
}
}
// ConvertBack not relevant here.
}
Now you'll have one converter to rule them all!
converter should return object which match requested targetType. converter can return different values for input enum value depending on parameter. I think it is more flexible than relying on targetType only.
public class SpecEnumConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Enum)
{
if ((string) parameter == "brush")
return "Red"; // return brush here!
// if not pre-defined parameter (null or any other), return description
return (int) value; // return enum description here!
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
usage:
<Label Content="{Binding MailSettingState, Converter={StaticResource
EnumConverterSpec}}"
BorderBrush="{Binding MailSettingState, Converter={StaticResource
EnumConverterSpec}, ConverterParameter='brush'}" />
I already commented above, but here's the solution.
<Label DataContext="{Binding MailSettingState, Converter={converters:EnumConverter}}" Content="{Binding Label}" BorderBrush="{Binding BorderBrush}"/>
public class EnumConverter: MarkupExtension, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var enumValue = (MailSettingsStateEnum) value;
return new ConvertedEnum { Label = enumValue.Description(),
BorderBrush = new BorderBrush()};
}
// ConvertBack not relevant here.
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
public class ConvertedEnum
{
public string Label {get; set;}
public BorderBrush {get; set;}
}
Separate converters still look prettier to me.
I'm trying to implement the MVVM design pattern in my WPF application but I have some problems to bind my Views with my ViewModels.
In one of my ViewModels, I have the following property :
public IPEndPoint EndPoint
{
get { return _serverInfos.EndPoint; }
private set
{
_serverInfos.EndPoint = value;
RaisePropertyChanged("EndPoint");
}
}
I want to bind this property in the related View like that :
<TextBox Text="{Binding EndPoint.Address}" />
<TextBox Text="{Binding EndPoint.Port}" />
The EndPoint.Port binding works as expected but the other one doesn't because EndPoint.Address is not a string (it's an IPAddress). Of course, I could define two string properties instead of one IPEndPoint but I think that it's not a good solution.
I also have the same problem using Enums when I want to convert them into int :
<ComboBox SelectedIndex="{Binding MyEnumProperty}" />
How could I solve these problems ?
Thank you for your help.
Normally you want a view model to take things from the model and expose them in a way that the view can consume. As such
MyEnumProperty should be an System.Int32 (int) for ComboBox.SelectedIndex to consumer
You should probably implement two separate properties for EndPointPort and EndPointAddress, and EndPointAddress should be a string that converts to an IPAddress when working with the model
You can use IValueConverters for both of those, but then you are reducing some of the utility of a separate view model in the first place if all it does is act like the model.
A converter you can use for converting between IPAddress and string.
public class IPAddressConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var ipAddress = value as IPAddress;
if (ipAddress != null)
{
return ipAddress.ToString();
}
return DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var text = value as string;
IPAddress ipAddress;
if (text != null && IPAddress.TryParse(text, out ipAddress))
{
return ipAddress;
}
return DependencyProperty.UnsetValue;
}
}
And then in a ResourceDictionary or a Resources collection of a FrameworkElement
<IPAddressConverter x:Key="IpAddressConverter" />
And in the binding:
<TextBox Text="{Binding EndPoint.Address, Converter={StaticResource IpAddressConverter}}" />
As far as the IPEndPoint type, this is a perfect case for a type converter (IValueConverter). It would look something like this, assuming your type has a valid ToString implementation:
public class IPEndPointConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
IPEndPoint endPoint = (IPEndPoint)value;
return endPoint.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
You could then add the converter to your XAML file with a ResourceDictionary as follows (assuming you've added the relevant namespace, here called "converters", to your XAML):
<Window.Resources>
<converters:IPEndPointConverter x:Key="ipEndPointConverter" />
</Window.Resources>
You would then simply use this anywhere you need it within your XAML by adding it to the binding:
<TextBox Text="{Binding Path=EndPoint.Address, Converter={StaticResource ResourceKey=ipEndPointConverter}}" />
How to convert this property to a dependency property ? Regarding that everybody just said that "Do not use logic in dependency property" and didn't proposed a remedy for that :
public DateTime? SelectedDateGeorgian
{
get
{
//choose a control based on this "user control" current mode
//and return its value
}
set
{
//choose a control based on this "user control" current mode
// and set its property after some manipulations on the value
}
}
I want to convert it to this :
public static readonly DependencyProperty SelectedDateGeorgianProperty =
DependencyProperty.Register("SelectedDateGeorgian", typeof(DateTime?), typeof(MyDatePicker), new PropertyMetadata(default(DateTime?)));
public DateTime? SelectedDateGeorgian
{
get
{
//Its prohobited to do something here ! So what should I do ?
//How should I select a control and return its value here ?
//I can't have a simple backing variable because I should do some conversion here
return (DateTime?)GetValue(SelectedDateGeorgianProperty);
}
set
{
//I want to convert received value here and
// and after that update some UI properties in this user control
SetValue(SelectedDateMiladiProperty, value);
}
}
I want to convert the value which is going to be written in this dependency property and also update UIElements.
And also I want to convert a value from an UIElement and return the converted value whenever it's going to be read.
So you see that I can't have a simple backing variable.
Please somebody give me a pattern to implement this.
Thanks for your attention.
Yes, you can.
You have to bind your UIElement property to this DependencyProperty and use a Converter. See How to: Convert Bound Data.
BTW: Here you can find the reason, why DependencyProperties shouldn't have additional logic in the property wrapper.
Edit:
<DatePicker Name="dp1"
SelectedDate="{Binding Path=SelectedDateGeorgian,
RelativeSource="{RelativeSource AncestorType=UserControl}"}" />
<DatePicker Name="dp2"
SelectedDate="{Binding Path=SelectedDateGeorgian,
RelativeSource="{RelativeSource AncestorType=UserControl}",
Converter={StaticResource dateConverter}}" />
create the converter:
[ValueConversion(typeof(DateTime?), typeof(DateTime?))]
public class DateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// your conversions
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// your backconversions
}
}
and add it as a resource:
<src:DateConverter x:Key="dateConverter"/>
Maybe I am not quite grasping multibindings.
I have a property on my viewmodel called OfficeDisplayName that is written to the database.
This is a concatenated field based on a person's FirstName, Lastname, and office location.
So I have a multibinding on a textBlock...no biggie...works beautifully...but how do I bind the full value of this concatenation to the OfficeDisplayName property? Do I have to have a hidden element that binds to the multibound textbox? I have seen several examples that are almost what I need, but just dont answer the concat databinding question.
One way is to let the textblock bind directly to OfficeDisplayName and then put the concatenation logic in the OfficeDisplayName property on your viewmodel instead of in the MultiValueConverter. So when ever one of the properties FirstName, LastName, or office location change you'd fire the PropertyChanged event for OfficeDisplayName - i.e. something along the following lines. This way you will not need a converter at all:
class YourViewModel : ViewModel
{
string _firstName;
public string FirstName
{
get { return _firstName; }
set
{
if (_firstName != value)
{
_firstName = value;
OnPropertyChanged("FirstName");
OnPropertyChanged("OfficeDisplayName");
}
}
}
// More properties here
// ...
public string OfficeDisplayName
{
get { return String.Join(" ", new string[] { _firstName, _lastName, _officeLocation}); }
}
}
Another way is to pass your viewmodel itself as a parameter to your MultiValueConverter. In your converter you can set the value of OfficeDisplayName directly. I think this way is a bit "hack-ish" but it is a matter of taste. Your code would look like the following:
The binding in XAML:
<MultiBinding Converter="{StaticResource theConverter}" Mode="OneWay">
<Binding /> <!-- Pass the datacontext as the first parameter -->
<Binding Path="FirstName" />
<Binding Path="LastName" />
</MultiBinding>
The converter:
class TheMultiValueConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var viewModel = values[0] as TheViewModel;
var ret = String.Join(" ", values.Skip(1).Cast<string>().ToArray());
viewModel.OfficeDisplayName = ret;
return ret;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
I just started playing around with Silverlight and a supposedly simple thing like binding a Combobox is driving me nuts. I read a bunch of articles now but none really address the issue that I'm after or were made for Silverlight 2 and don't seem to work.
Let's say I have an entity object "User" which has a "UserStatus" field. In the database UserStatus field is defined as byte and in code it's defined as:
public enum UserStatus : byte
{
Active = 1,
Locked = 2,
Suspended = 3,
}
When ADO.NET entity framework creates the user entity it leaves the UserStatus field as byte. So, to address this I stumbled across IValueConverter and implemented the following:
public class EnumConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch (parameter.ToString())
{
case "UserStatus":
return ((UserStatus)value).ToString();;
}
return "?";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
Now, I also need to supply the Combobox an ItemSource, so I implemented this:
internal static class EnumValueCache
{
private static readonly IDictionary<Type, object[]> Cache = new Dictionary<Type, object[]>();
public static object[] GetValues(Type type)
{
if (!type.IsEnum)
throw new ArgumentException("Type '" + type.Name + "' is not an enum");
object[] values;
if (!Cache.TryGetValue(type, out values))
{
values = type.GetFields()
.Where(f => f.IsLiteral)
.Select(f => f.GetValue(null))
.ToArray();
Cache[type] = values;
}
return values;
}
}
public class EnumValuesConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return null;
switch (parameter.ToString())
{
case "UserStatus":
return EnumValueCache.GetValues(typeof(UserStatus));
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
And then finally, this is how I added it to my XAML:
<ComboBox ItemsSource="{Binding UserStatus, Mode=TwoWay, Converter={StaticResource EnumValuesConverter}, ConverterParameter='UserStatus'}"
SelectedItem="{Binding UserStatus, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter='UserStatus'}" />
What happens now though is that the ItemsSource gets correctly bound and I see all the options in the dropdown, however, the SelectedItem is not set.
Even when I try to manually set SelectedItem to ="1" or "Active", none of them work.
Can anyone help me out and tell me what's wrong, why I can't seem to get the SelectedItem set?
Thanks,
Tom
I can see two problems with your code.
First, in silverlight 3 the ComboBox matches the object in the SelectedItem to the set of objects in the ItemsSource via the object.Equals method. However your GetValues method returns an array of Boxed enum values. Whereas your EnumConverter returns a string. Hence you asking Silverlight to compare a byte with a string, these are never equal.
Secondly, you need to place some code in the ConvertBack method if you are going to two way bind the SelectedItem (BTW there is no need for a twoway binding the ItemsSource).