How to add logic to dependency property? - wpf

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"/>

Related

WPF MVVM binding with specific types

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}}" />

Using WPF TextBox with a URI converter, invalid input wipes textbox

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>

Silverlight 3 - binding enum to combobox from DomainServiceContext

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).

WPF databinding binding error notification

OK, working on WPF(using MVVM) and came across a question, want some input. I have a simple class
like below(assume I have IDataErrorInfo implemented):
public class SimpleClassViewModel
{
DataModel Model {get;set;}
public int Fee {get { return Model.Fee;} set { Model.Fee = value;}}
}
I then try to bind to it in xaml:
<TextBox Text={Binding Fee, ValidatesOnDataErrors=true}/>
when a user then clears out the text, a databinding error occurs because it cant convert string.empty to int. Well, Fee is a required field, but because the databinding won't convert back I can't provide error information because my class isn't updated. So am I required to then do the following?
public class SimpleClassViewModel
{
DataModel Model {get;set;}
int? _Fee;
public int? Fee
{
get { return _Fee;}
set { _Fee = value;if (value.HasValue) { Model.Fee = value;}
}
}
This can be done using a ValueConverter:
using System.Windows.Data;
namespace MyNameSpace
{
class IntToStringConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((int) value).ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
int result;
var succes = int.TryParse((string) value,out result);
return succes ? result : 0;
}
}
}
You reference it in the XAML thus:
<Window xmlns:local="clr-namespace:MyNameSpace">
<Window.Resources>
<local:IntToStringConverter x:Key="IntConverter"/>
</Window.Resources>
<TextBox Text={Binding Fee, ValidatesOnDataErrors=true,
Converter={StaticResource IntConverter}}/>
</Window>
You can also take advantage of the fact you're doing MVVM and change the type of the Fee property to string. After all, your VM should provide a model that supports the view, and the view allows users to enter a string. Then you can provide a separate property that exposes the parsed fee as an int. That way your conversion logic is right there in the Fee property, making it easier to reuse, debug, and maintain.

Bound Property always returns false but WPF bound CheckBox is still checked

I have a checkbox in a UserControl:
<CheckBox Content="Existing" IsChecked="{Binding Path=IsExistingTemplate, Mode=TwoWay}"/>
It is bound to a DataContext with Property IsExistingTemplate which always returns False. (In my real application, it doesn't always return False!). DataContext implements INotifyPropertyChanged.
public bool? IsExistingTemplate
{
get
{
return false;
}
set
{
OnPropertyChanged("IsExistingTemplate")
}
}
When the user clicks the CheckBox, the CheckBox always shows a tick.
How can I force the CheckBox not to show a tick when the user Clicks it?
I remember having a very similar problem, but I can't find where and how I solved it. I thought that the problem was caused by how WPF updates bindings, and I thought that I read somewhere that it could be solved by using an IValueConverter that simply passes the values through, but I've tested that it and it doesn't seem to work. It was supposed to work because using a converter was supposed to make the bindings re-evaluate.
You could always handle the Clicked event on the CheckBox and update the values via code, but it just feels so dirty. Sorry I couldn't be of more help
<StackPanel.Resources>
<local:PassThroughConverter x:Key="PassThroughConverter" />
</StackPanel.Resources>
<CheckBox IsChecked="{Binding IsExistingTemplate, Converter={StaticResource PassThroughConverter}}" Content="Stuff"/>
public class PassThroughConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}

Resources