How to set a nullable property value by Style setter? - c#

I am developing a Windows Store (8.1) application.
I have this control which has a nullable int property (type "int?"), when I try to set it to an int via Style, I get an error.
<Setter Property="Minimum" Value="0" />
Error Cannot assign to nullable type on property Minimum
Any ideas?

Following sample depicts solution, adjust it to your demands.
C#:
class IsNullToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value == null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<Window.Resources>
<local:IsNullToBoolConverter x:Key="IsNullToBoolConverter"/>
</Window.Resources>
<TextBlock Text="Text">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Age, Converter={StaticResource IsNullToBoolConverter}}" Value="False">
<Setter Property="FontSize" Value="55"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>

you can achieve this by using TargetNullValue property. Use below code,
TargetNullValue={x:Static sys:String.Empty}
Note :
sys is the imported xml namespace for System in mscorlib:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Hope this will help..

Related

Converter to Bool

I have a normal Checkbox, where I want to set the IsChecked property to a Binding resource.
The resource is a self written class myClass, which can be null or referenced (means not null).
The Checkbox should be NOT checked, if the assigned object myObject (out of myClass) is null
and checked, if it is not null.
What do I have to write in the IsChecked="..." property in my xaml file?
You can create a style with a DataTrigger that sets the IsChecked property.
<CheckBox>
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
<Setter Property="IsChecked" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding MyObject}" Value="{x:Null}">
<Setter Property="IsChecked" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
An alternative is to create a reusable value converter.
public class NotNullToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value != null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new InvalidOperationException();
}
}
Create an instance of the converter in any resource dictionary, e.g. application resources.
<local:NotNullToBooleanConverter x:Key="NotNullToBooleanConverter"/>
This converter can be used directly in the binding.
<CheckBox IsChecked="{Binding MyObject, Converter={StaticResource NotNullToBooleanConverter}}"/>

WPF ListBox showing empty row created by DataGrid

I have both DataGrid and Listbox binded to the same ObservableCollection:
public ObservableCollection<Contact> contacts = new ObservableCollection<Contact>();
CntGrid.ItemsSource = contacts;
CntListBox.ItemsSource = contacts;
<DataGrid x:Name="CntGrid"
IsReadOnly="False"
CanUserAddRows="True"
CanUserDeleteRows="True"/>
<ListBox x:Name="CntListBox"/>
The problem is DataGrid allowing adding items (I want to keep this functionality) causes ListBox to display an empty row aswell. I don't want my ListBox to display this empty row at the end.
Can I somehow modify my ListBox to fix this?
This will hide the {NewItemPlaceholder} item in your ListBox:
<ListBox x:Name="CntListBox">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="{x:Static CollectionView.NewItemPlaceholder}">
<Setter Property="UIElement.Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Empty row in DataGrid is a NewItemPlaceholder. It has different type from Contact. So I suggest to use a converter to hide it in ListBox:
public class ObjectTypeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var t = parameter as Type;
if (value == null || t == null)
return false;
return t.IsAssignableFrom(value.GetType());
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
}
use that converter in ListBoxItem Style to check the type of item and hide them if type doesn't match:
<Window.Resources>
<local:ObjectTypeConverter x:Key="tc"/>
</Window.Resources>
<ListBox x:Name="CntListBox">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<DataTrigger Binding="{Binding Converter={StaticResource tc}, ConverterParameter={x:Type local:Contact}}"
Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

Telerik WPF GridView header text disappears when content is bound to tooltip

I am following Telerik guide for displaying a tooltip on WPF GridView column headers. The task is to create a tooltip which would have same text as column header.
In generic.xml there is this style:
<Style TargetType="telerik:GridViewHeaderCell">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content}" />
</Style>
This does work and shows a tooltip when mouse is hovered over column header, however the actual header is being cleared.
I wonder why this is happening and how to fix it? GridViewHeaderCell inherits from ContentControl.
I ended up adding a converter because "Content" value appeared to be a TextBlock:
public class TooltipObject : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
return ((TextBlock)value).Text;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
and the style looks like:
<utils:TooltipObject x:Key="tooltip" />
...
<Style TargetType="telerik:GridViewHeaderCell">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content, Mode=TwoWay, Converter={StaticResource tooltip}}" />
</Style>

WPF Value Converter to replace error code with localized string

I have a viewmodel implementing the IDataErrorInfo interface for validation, for an error it produces a string which is a key in the resource dictionary for a localized string describing the error. However when trying to apply the following style and template to the textbox I get the red border but no tooltip, however removing my converter and using the default one gives me the tooltip but obviously not the localized string.
Can you see what I am doing wrong and/or if there is a better way of doing this?
class MessageCodeToMessageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
string messageCode = (string)value;
try
{
return (string)App.Current.Resources[messageCode];
}
catch (Exception)
{
return messageCode;
}
}
else
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return "";
}
}
<local:MessageCodeToMessageConverter x:Key="Converter"></local:MessageCodeToMessageConverter>
<Style x:Key="TextBox" TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent, Converter={StaticResource Converter}}"/>
</Trigger>
</Style.Triggers>
</Style>
<ControlTemplate x:Key="ErrorTemplate">
<DockPanel LastChildFill="True">
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
In my opinion if your resource key is defined in the App.xaml then it should work. But your resource key is probably in resource in some user control. (string)App.Current.Resources[messageCode]; search only in the App.xaml resource.
Solution for you can be use multivalueconverter
class MessageCodeToMessageConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
FrameworkElement targetObject = values[0] as FrameworkElement;
if (targetObject == null)
{
return DependencyProperty.UnsetValue;
}
if (value != null)
{
string messageCode = (string)values[1];
try
{
return targetObject.FindResource(messageCode);
}
catch (Exception)
{
return messageCode;
}
}
else
{
return null;
}
}
public object ConvertBack(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return "";
}
}
and
<Style x:Key="TextBox" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip">
<Setter.Value>
<MultiBinding Converter="{StaticResource Converter}">
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}" />
<Binding RelativeSource="{x:Static RelativeSource.Self}" Path="(Validation.Errors)[0].ErrorContent" />
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>

Check in Data Trigger if object is particular class

Is there a way to check in DataTrigger if the object is of a particular class?
In fact I would like that DataTrigger answer this question in C#:
if(MyObject is MyClass)
I want it to look something like this in XAML:
<Grid>
<Grid.Triggers>
<DataTrigger Binding="{Binding MyObject}" Value="MyClass?">
<Setter..../>
</DataTrigger>
</Grid.Triggers>
</Grid>
You can use a converter for this:
<Grid>
<Grid.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding MyString, Converter={StaticResource OConv}, ConverterParameter=System.String}" Value="True">
<Setter Property="Grid.Background" Value="Red"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
Use the ConverterParameter to state the type of the object you expect to receive...
Converter will return true if it matches or false otherwise...
Example of converter:
public clas s ObjectTypeToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value.GetType().ToString() == (string)parameter)
{
return true;
}
return false;
}

Categories

Resources