Clearing Textbox does not set binding to null - wpf

I am having difficulty setting an empty TextBox to null on an nullable DB Field.
XAML
<y:TextBox Text="{Binding Year1Cost, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
NotifyOnValidationError=True,
ValidatesOnDataErrors=True,
ValidatesOnExceptions=True,
StringFormat=\{0:c\}}" Grid.Row="3" Grid.Column="1" />
When I enter any value into it the binding is fine and the entered value is passed
When I leave a null value alone a null is passed
If I delete the value from the TextBox the value passed is the Original value of the Textbox and the UI is not notified of the change Grrrrrrrrrrrrrrrr
I have spent a long time checking out options
nothing short of putting code behind the OnTextChanged of every nullable field I cannot see the efficiency in doing this.
Thanks in advance:
ps. Have looked at TargetNullValue to no success
Visual Studio 2008 - SP1 - .Net 3.5

Set the property TargetNullValue of the binding to String.Empty:
<TextBox Text="{Binding TargetNullValue={x:Static sys:String.Empty}}"/>
I tried it and it works for me.
And if I am not wrong (please forgive me if I am), you should set the property StringFormat like this:
StringFormat={}{0:C}
This is even probably the reason of the exception you got.

Consider using a value converter. You should be able to implement the ConvertBack method to translate empty strings into nulls.

For me only converter works :
Here's a link
public class NullableConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? string.Empty : String.Format(culture, "{0}", value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.IsNullOrEmpty(String.Format(culture, "{0}", value)) ? null : value;
}
}

Related

Flexing date format in XAML at runtime

Based on a setting in my application I need to be able to show different formats of showing a Date in XAML. For example it needs to be either in "yyyy/MM/dd" format or in "MM/dd/yyyy" format.
What are some ways that I can achieve this? Please note that the places in my application that I need to handle this are limited so even a "fix on the spot" type of solution would work in my case too.
Thanks for suggestions.
You can use a ValueConverter for this. Just replace YourApplicationSettings.YearFirst with wherever you're pulling the setting from.
public class DateTime2FlexibleDateString : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var dt = value as DateTime?
if (dt== null)
{
return null;
}
var dateTime = dt.Value;
if (YourApplicationSettings.YearFirst)
{
return dateTime.ToString("yyyy/MM/dd");
}
return dateTime.ToString("MM/dd/yyyy");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
Then either in each xaml file that needs it, or in a App.xaml (or a resource dictionary added to App.xaml), add a static resource for your converter.
<UserControl.Resources>
<converters:DateTime2FlexibleDateString x:Key="DateTime2FlexibleDateString" />
</UserControl.Resources>
And then when you bind to the date, specify the converter.
<TextBlock Text="{Binding MyDateTimeVariable, Converter={StaticResource DateTime2FlexibleDateString}" />
It depends on the context of what you are doing, but if you are binding to a DateTime then you can alter the StringFormat property like so:
<TextBlock Text="{Binding MyDateTimeVariable, StringFormat='yyyy/MM/dd'}" />
<TextBlock Text="{Binding MyDateTimeVariable, StringFormat='MM/dd/yyyy'}" />
Here is a good example of some different format options that be achieved like this.

Writing conditional statements in XAML code

I have this listBox that gets populated, each item can be either male or female depending on the 'SEX' property that is binded to the listBox. (Could be either 'M' for male and 'F' for female)...
For each item i would like to display either a male or female symbol based on the list items SEX property.
for instance if "{Binding SEX}" equals 'M':
<Image Source="../Images/male48.png" Visibility="Visible" />
and if "{Binding SEX}" equals 'F':
<Image Source="../Images/female48.png" Visibility="Visible" />
How exactly would I go about getting this to work?
A common approach to this problem is to create a value converter, this converts the value returned by a binding into some other value that relates to the property of a UI control.
You can create a converter that takes the sex and maps it to an image source:
public class SexToSourceConverter : IValueConverter
{
public object Convert(object value, string typeName, object parameter, string language)
{
string url = ((string)value == "M") ? "../Images/male48.png" : "../Images/female48.png";
return new BitmapImage(new Uri(url , UriKind.Relative));
}
public object ConvertBack(object value, string typeName, object parameter, string language)
{
throw new NotImplementedException();
}
}
Using it in your XAML as follows:
<Image Source="{Binding Path=Sex, Converter={StaticResource SexToSourceConverter }" />
If someone is interested in how this could work, I've made a solution based on ColinE's answer. First, you've to create a new class which contain the conditions you'll like to add to the XAML code:
public class MyNiceConverterName : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
// Your conditions here!
return value_you_want_to_return; // E.g., a string, an integer and so on
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException(); // Leave this like here, don't worry
}
}
Call the class whatever you want (right now it's called MyNiceConverterName) and implement the Convert() method with the conditions you'd like to add to the XAML file. Remember to cast the object value to the type you're using (e.g., (int)value if it's an integer).
This is almost done! But not yet, first declare the converter in your XAML as a resource. You can paste this code below the namespaces declaration:
<Control.Resources>
<converter:MyNiceConverterName xmlns:converter="clr-namespace:My_Namespace" x:Key="MyNiceConverterName" />
</Control.Resources>
You've to declare the namespace where you defined the class (i.e., remove My_Namespace with yours') and also rename MyNiceConverterName to your class name. The key will be the name defined to reference the converter within the XAML document, here I've used the same class name but you can freely change it.
Finally, it's time to use the converter. Put this and you're done:
{Binding variable_with_value, Converter={StaticResource MyNiceConverterName}}
Remember to change variable_with_value with the one you'd like to use within your binding.
I hope it helps!
Either use a binding converter or use two triggers.
For Siverlight this is the correct IValueConverter link, I am not sure if triggers are supported.

WPF conditional convertor

I have binding to Image.Source and TextBlocks. Also I have own convertor classes.
<conv:StatusConvertor x:Key="statusConvertor"></conv:StatusConvertor>
<conv:ImageConvertor x:Key="imageConvertor"></conv:ImageConvertor>
For example:
<Image Source="{Binding Value.profilePhoto, Converter={StaticResource imageConvertor}}" Margin="4,4,4,2"/>
<TextBlock Name="tbStatus" Text="{Binding Value.status,Converter={StaticResource statusConvertor}}" Grid.Column="0" Grid.Row="2" Margin="2,2,2,2" FontSize="11" FontWeight="Normal"></TextBlock>
I want set condition for imageConvertor, for example:
IF tbStatus.Text=="0"
THEN use imageConvertor on Image.Source
It’s possible this write in XAML, maybe in convertor class?
Instead of making your ImageConverter an IvalueConverter, make it an IMultiValueConverter:
<Image Margin="4,4,4,2">
<Image.Source>
<MultiBinding Converter="{StaticResource imageConvertor}">
<Binding Path="Value.profilePhoto" />
<Binding Path="Value.status" />
</MultiBinding>
</Image.Source>
</Image>
A IMultiValueConverter is the same as an IValueConverter, except that it passes an array of objects instead of a single object value.
public object Convert(object[] values,
Type targetType, object parameter, CultureInfo culture)
{
// Use your converter code from before, but add a check for the Status value
// as well
string path = values[0].ToString();
int status = Int32.Parse(values[1].ToString();
if (status == 0)
return newImageSource;
return DependencyProperty.UnsetValue;
}
Here is is tough for me to guess on the design of your current converter, but this gives you a rough idea on what to do. I am implying from your question that if the status is not 0, you don't want your converter to return anything - hence the DependencyProperty.UnsetValue.
I don't think it's possible to do this in XAML.
I'm pretty sure it's impossible to do (as is) in the converter because you don't have access to the sender (here a TextBlock) within.
EDIT : you can do it with a multivalue converter, because you need OneWay Binding. Multivalue converters are difficult to set up with a TwoWay binding (when you need the ConvertBack method).
What I would do would be to declare two Images (one with for each value of your TextBlock : 0 and else) and bind the visibility to the textblock Text value (or directly to Value.status).
This doesn't specifically answer the question, but I managed to solve my problem using this question as a guide, so I thought it might help future searchers. It could probably also be extended further to solve the original question with a little more work.
I was trying to find a way of evaluating an IF-conditional expression within XAML and wanted to combine the power of a Binding expression and a MarkupExtension. Using the idea of a converter, I managed to create a ConditionalMarkupConverter. This has two properties to specify the values to be returned when a binding expression evaluates to true or false, and since it's a converter, it can be easily added to a binding expression.
Converter
public sealed class ConditionalMarkupConverter : MarkupExtension, IValueConverter
{
public object TrueValue { get; set; }
public object FalseValue { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool && (bool)value)
{
// The given value has evaluated to true, so return the true value
return TrueValue;
}
// If we get here, the given value does not evaluate to true, so default to the false value
return FalseValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
XAML
<TextBlock Text="{Binding IsActive, Converter={converters:ConditionalMarkupConverter TrueValue=Active, FalseValue=Inactive}}" />
In this example, the TextBlock binds to a boolean property called IsActive and the converter then returns the string "Active" when IsActive is true, or "Inactive" when IsActive is false.

WPF - Dynamically access a specific item of a collection in XAML

I have a data source ('SampleAppearanceDefinitions'), which holds a single collection ('Definitions'). Each item in the collection has several properties, including Color, which is what I'm interested in here.
I want, in XAML, to display the Color of a particular item in the collection as text. I can do this just fine using this code below...
Text="{Binding Source={StaticResource SampleAppearanceDefinitions}, Path=Definitions[0].Color}"
The only problem is, this requires me to hard-code the index of the item in the Definitions collection (I've used 0 in the example above). What I want to do in fact is to get that value from a property in my current DataContext ('AppearanceID'). One might imagine the correct code to look like this....
Text="{Binding Source={StaticResource SampleAppearanceDefinitions}, Path=Definitions[{Binding AppearanceID}].Color}"
...but of course, this is wrong.
Can anyone tell me what the correct way to do this is? Is it possible in XAML only? It feels like it ought to be, but I can't work out or find how to do it.
Any help would be greatly appreciated!
Thanks!
AT
MultiBinding is your friend here:
Assuming you have a TextBlock:
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource AppearanceIDConverter}">
<Binding Source="{StaticResource SampleAppearanceDefinitions}" />
<Binding Path="AppearanceID" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
And define a MultiValueConverter to return what you wish to see:
public class AppearanceIDConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
List<item> items = (List<item>)values[0]; //Assuming its items in a List
int id = (int)values[1]; //Assuming AppearanceID is an integer
return items.First(i => i.ID == id).Color; //Select your item based on the appearanceID.. I used LINQ, but a foreach will work just fine as well
}
public object[] ConvertBack(object value, System.Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
#endregion
}
Of course, you will need to set the converter as a resource in your Resource dictionary, like you did SampleAppearanceDefinitions. You can also ditch the multibinding and use a regular binding to AppearanceID with a IValueConverter, if you can get to the SampleAppearanceDefinitions collection through code ;).
Hope this helps
Even if it could be possible you'd better not do that this way, but instead use a dedicated property in your view model or in the code behind of your view if it has only a pure graphical meaning.
This property, say "CurrentAppearance", would expose a Color property you could bind from your Xaml :
Text="{Binding CurrentAppearance.Color}"
which is more understandable.
As a general advice : avoid to spoil your Xaml with plumbing code : Xaml should be as readable as possible,
particularly if you work with a team of designers that have no coding skills and do not want to be concerned with the way you are managing the data.
Moreover, if later you decide to change the way data are managed you would not have to change your Xaml.
MultiBinding might actually work if your list is on a viewmodel instead of a staticresource. I was suprised myself to see that the object passed on to the view is actually a pointer to the object on the model, so changing the object in the view (eg. typing in new test in the textbox) directly affects the model object.
This worked for me. The ConvertBack method is never useed.
public class PropertyIdToPropertyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length == 2)
{
var properties = values[0] as ObservableCollection<PropertyModel>;
if (properties != null)
{
var id = (int)values[1];
return properties.Where(model => model.Id == id).FirstOrDefault();
}
}
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

Silverlight DatePicker in DataGrid allow NULL dates

I have a DataGrid that has as one of its columns a DatePicker (for the editing template). Is there a way to keep this but still allow a user to enter in a NULL date?
Do the following (in Silverlight 4):
1) Bound property of DataContext must be Nullable (or Datetime?)
2) In XAML set TargetNullValue='' of binding property something like:
Text="{Binding DocumentDate, Mode=TwoWay, StringFormat='yyyy-MM-dd', TargetNullValue=''}"
Found this while researching same problem. This is how I fixed it since it hasn't been marked.
I have used converter which converts value to null if it is just an empty datatime object.
public class DateTimeToNullConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime dt = new DateTime();
if (dt.Equals(value)) return null;
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
XAML:
SelectedValue="{Binding CurrentContractRenewal.ExpiryDate, Mode=TwoWay, Converter={StaticResource DateTimeToNullConverter}}"

Resources