Conversion from ReadOnlyObservableCollection to List<MyClass> - c#

I have a datagrid with grouping and I am trying to style the template to add in some summary about the group.
IN XAML DataGrid.RowGroupHeaderStyle
<Grid Grid.Column="3" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="0,1,0,1" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Name}" Margin="4,0,0,0" />
<TextBlock Grid.Column="1" Text="{Binding Items, Converter="{StaticResource summaryConverter}"}" />
</Grid>
Binding of data grid items source
PagedCollectionView collection = new PagedCollectionView(e.Result.ToList<MyClass>());
collection.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
dataGrid.ItemsSource = collection;
MyClass
class MyClass {
public string Name;
public double Value;
}
I have created a converter to grab the Items of the same group but I am facing problem in converting the object into List<MyClass>(). I receive this error
Unable to cast object of type 'System.Collections.ObjectModel.ReadOnlyObservableCollection1[System.Object]' to type 'System.Collections.Generic.List1[MyClass]'.
`
In Converter.cs
public object Convert(object values, Type targetType, object param, CultureInfo culture) {
var source = (List<MyClass>)values;
}
Does anyone know how should I do the conversion??

Use LINQ.
public object Convert(object values, Type targetType, object param, CultureInfo culture) {
var source = (ReadOnlyObservableCollection<object>) values;
List<MyClass> list = source.OfType<MyClass>().ToList();
...
}

Related

Show XAML on Condition

The context is I have a Listbox that is outputting items with quite a lot of styling (more than i've put here)... I went to use a DataTemplateSelector to show either one image or another based on a condition but it was stupid to have to re-write the whole tempalte with just this one difference in it.
I have placed some pseudo code to demonstrate what i'm trying to achieve:
<ListBox Grid.Row="1"
ItemsSource="{Binding TestCases}"
BorderThickness="0"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Padding="0,5,0,5">
<Run Text="{Binding Class}"></Run>
</TextBlock>
<TextBlock Grid.Column="1"
Padding="5">
<Run Text="{Binding Assertions}"></Run>
</TextBlock>
if {Binding Failures} > 0 {
<Image HorizontalAlignment="Center"
Width="10"
Height="10"
Grid.Column="2"
Source="/Content/Images/Cross.png">
</Image>
}
else
{
<Image HorizontalAlignment="Center"
Width="10"
Height="10"
Grid.Column="2"
Source="/Content/Images/Tick.png">
</Image>
}
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Any ideas on how i do it?
-------------Edit------------
So far i've added the namespace of my converter class to the XAML file:
xmlns:converters="clr-namespace:Blah.Blah.Converters"
Then added window resources:
<Window.Resources>
<converters:FailuresTickConverter x:Key="failuresTickConverter" />
<converters:FailuresCrossConverter x:Key="failuresCrossConverter" />
</Window.Resources>
Then i have the classes themselves:
namespace Blah.Blah.Converters
{
public class FailuresTickConverter : IValueConverter
{
public object Convert( object value, Type targetType,
object parameter, CultureInfo culture )
{
int failures = (int)value;
if( failures > 0 )
return Visibility.Hidden;
return Visibility.Visible;
}
public object ConvertBack( object value, Type targetType,
object parameter, CultureInfo culture )
{
throw new NotImplementedException();
}
}
public class FailuresCrossConverter : IValueConverter
{
public object Convert( object value, Type targetType,
object parameter, CultureInfo culture )
{
int failures = ( int )value;
if( failures > 0 )
return Visibility.Visible;
return Visibility.Hidden;
}
public object ConvertBack( object value, Type targetType,
object parameter, CultureInfo culture )
{
throw new NotImplementedException();
}
}
}
Then in my XAML on the images i've done this:
<Image Visibility="{Binding Failures, Converter=failuresTickConverter}"
HorizontalAlignment="Center"
Width="10"
Height="10"
Grid.Column="2"
Grid.Row="0"
Source="/Content/Images/Tick.png">
</Image>
<Image Visibility="{Binding Failures, Converter=failuresCrossConverter}"
HorizontalAlignment="Center"
Width="10"
Height="10"
Grid.Column="2"
Grid.Row="0"
Source="/Content/Images/Cross.png">
</Image>
I'm getting an error on the bindings:
IValueConverter does not support converting from string
Elaborating on my comment, here's a simple (not the only one, of course) way to do it:
Create a converter to convert number to visibility:
public class PositiveToVisibilityConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (int)value > 0 ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Add the namespace of the converter to your xaml, add the converter to resources (use your own namespace of course):
<Window ...
xmlns:converters="clr-namespace:WpfApplication1.Converters"
...>
<Window.Resources>
<converters:PositiveToVisibilityConverter x:Key="PositiveConverter"/>
</Window.Resources>
Then put both the images in, the "success" one first and bind the property using the converter to the "failure" one:
<Image HorizontalAlignment="Center"
Width="10"
Height="10"
Grid.Column="2"
Source="/Content/Images/Tick.png"
Visibility="{Binding Failures, Converter={StaticResource PositiveConverter}}">
</Image>
<Image HorizontalAlignment="Center"
Width="10"
Height="10"
Grid.Column="2"
Source="/Content/Images/Cross.png">
</Image>
Since these images are in a grid (I assume) and have the same position and size, they will overlap and normally the one that's defined last will be drawn last, so the first one will not be visible. That is, unless the binding makes the second image invisible.

Xaml ResourceKey as a variable not working

I'm using a ComboBox (WPF 4.0) to show user defined paragraph styles for an editor app. The ComboBox has two columns:
(1) Name of the paragraph style
(2) Text "abcABC123", should in some properties be formatted according to the paragraph style in the first column
(1) is working, (2) is not because _ResourceKey_background, _ResourceKey_foreground and _ResourceKey_fontFamily are no ResourceKeys
but variables containing ResourceKeys. How can I solve this?
_NameInternal, _NameUI, _ResourceKey_background, _ResourceKey_foreground and _ResourceKey_fontFamily are public properties
of the user defined paragraph style class.
<ComboBox Name="_cbStylesPara" SelectedValuePath="_NameInternal"
ItemsSource="{Binding Source={StaticResource _collectionViewSource_stylesPara}}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Text="{Binding _NameUI}" VerticalAlignment="Center" />
<TextBlock Grid.Column="1" Text="abcABC123" Margin="3,0,0,0" VerticalAlignment="Center"
Background="{DynamicResource _ResourceKey_background}"
Foreground="{DynamicResource _ResourceKey_foreground}"
FontFamily="{DynamicResource _ResourceKey_fontFamily}" />
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
As like you said resources are declared under App resources, what you can do is create an IValueConverter and return the resource value from it's Convert method.
public class ResouceLookupConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return App.Current.TryFindResource(value);
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}
XAML:
<ComboBox>
<ComboBox.Resources>
<local:ResouceLookupConverter x:Key="ResouceLookupConverter"/>
</ComboBox.Resources>
......
<TextBlock Grid.Column="1" Text="abcABC123" Margin="3,0,0,0"
VerticalAlignment="Center"
Background="{Binding _ResourceKey_background,
Converter={StaticResource ResouceLookupConverter}}"
Foreground="{Binding _ResourceKey_foreground,
Converter={StaticResource ResouceLookupConverter}}"
FontFamily="{Binding _ResourceKey_fontFamily,
Converter={StaticResource ResouceLookupConverter}}" />
</ComboBox>
Note: Ofcourse you have to define local namespace in your XAML set to the namespace where your Converter is declared.

Change image in xaml based on a value from another xaml

I have a Views/Doc.xaml with:
<navigation:Page ....
<data:DataGrid>
<data:DataGridTemplateColumn Header="Actions" HeaderStyle="{StaticResource TextHeaderStyle}" >
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid x:Name="gridDocumentColumns">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<HyperlinkButton x:Name="hlEmail" Grid.Column="1" Tag="{Binding Index}" Click="hlEmail_Click" >
<ToolTipService.ToolTip>
<ToolTip Tag="ToolTipEmail" Opened="toolTip_ActionOpened" />
</ToolTipService.ToolTip>
<Image Source="../images/close.png" Stretch="None" />
</HyperlinkButton>
</Grid>
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid>
..............
I have a second class Views/Controls/ReadDocs.xaml (). If a certain condition in ReadDocs.xaml Code Behind is true i want to change the image source in Views/Doc.xaml to ../images/open.png
How can i achieve this?
You can define a converter and pass a flag value to it.
This converter will returns a specific path depending upon the value you have passed.
Kindly refer the below converter for reference...
public sealed class ImagetoPathConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
{
return value = "ms-appx:///Assets/Images/bk/1.png";
}
else if (value.ToString() == "1")
{
return value = "ms-appx:///Assets/Images/bk/2.png";
}
else if (value.ToString() == "2")
{
return value = "ms-appx:///Assets/Images/bk/3.png";
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
Bind the converter in Source and pass your conditional value.

Image not visible when setting source to ValueConverter

I'm trying to change an Image in a LongListSelector on basis of a boolean property of the DataContext of the LongListSelector. I'm using a ValueConverter in order to achieve this, and the code does reach the ValueConverter and returns a BitmapImage but this isn't visible on screen. Here's some of the relevant code:
XAML code (ItemTemplate and the ValueConverter declaration):
<local:BoolToImage x:Key="BoolImageConverter"/>
DataTemplate x:Key="itemTemplate">
<Grid Margin="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Foreground="Black" Margin="0" FontFamily="Segoe WP Light" FontSize="29.333" VerticalAlignment="Center" Text="{Binding BeginTijdTimeOnly}" />
<TextBlock Grid.Column="1" TextWrapping="Wrap" Foreground="Black" Margin="0" FontFamily="/LimburgsLeed;component/Fonts/Fonts.zip#Champion" FontSize="48" VerticalAlignment="Center" Text="{Binding Artiest.Naam}" />
<Image x:Name="image" Grid.Column="2" Source="{Binding Path=isSaved, Converter={StaticResource BoolImageConverter}}" VerticalAlignment="Center" Margin="0, 0, -1, 0" MouseLeftButtonDown="fav_Click"/>
</Grid>
</DataTemplate>
As you can see the Image is bound to the ValueConverter and the isSaved property.
ValueConverter code:
public class BoolToImage : IValueConverter
{
public BitmapImage TrueImage = new BitmapImage();
public BitmapImage FalseImage = new BitmapImage();
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
TrueImage.UriSource = new Uri("/Images/ThumbSelected#2x.png", UriKind.RelativeOrAbsolute);
FalseImage.UriSource = new Uri("/Images/thumb.png", UriKind.RelativeOrAbsolute);
if (!(value is bool))
{
return this.FalseImage;
}
bool b = (bool)value;
if (b)
{
return this.TrueImage;
}
else
{
return this.FalseImage;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
I don't have a clue why the image isn't shown.. The ValueConverter CAN'T return null.
My god, I sure feel stupid now. This all works fine, but I set the Image files their Build Action to Embedded Resource. Changing it to content resolved the problem.

How to convert back from string to Enum

I have a a class which is made of property information of a target(like type and values ). Iam using this UI to show all types on view in graphical format like Enum with comboboxes and boolean with checkboxes.Everything works well for me except the case when I change combobox value in UI,it does not change in viewmodel.Every time I change value in combobox it calls convertback method in my converter.I need to convert this string to enum type,I can write the convertback code easily for a particular enum type ,but how can I convert all other enums with this converter,I have the information of Type in PropertyType property that I can pass to converter and use it but I have no idea how to do it.
This is my UI code (only relevant part)
<!-- Default DataTemplate -->
<DataTemplate x:Key="DefaultDataTemplate">
<Grid Margin="4" MinHeight="25">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Key" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBox Margin="8,0" Grid.Column="1" Text="{Binding Value}" />
</Grid>
</DataTemplate>
<!-- DataTemplate for Booleans -->
<DataTemplate x:Key="BooleanDataTemplate">
<Grid Margin="4" MinHeight="25">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Key" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<CheckBox Margin="8,0" Grid.Column="1" IsChecked="{Binding Value}" />
</Grid>
</DataTemplate>
<!-- DataTemplate for Enums -->
<DataTemplate x:Key="EnumDataTemplate">
<Grid Margin="4" MinHeight="25">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Key" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<ComboBox Margin="8,0" SelectedItem="{Binding Value,Converter={StaticResource EnumToStringConverter},Mode=TwoWay}"
ItemsSource="{Binding PropertyType,
Converter={local:EnumToListConverter}}" Grid.Column="1"
HorizontalAlignment="Stretch" />
</Grid>
</DataTemplate>
<!-- DataTemplate Selector -->
<local:PropertyDataTemplateSelector x:Key="templateSelector"
DefaultnDataTemplate="{StaticResource DefaultDataTemplate}"
BooleanDataTemplate="{StaticResource BooleanDataTemplate}"
EnumDataTemplate="{StaticResource EnumDataTemplate}"/>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<ListView Grid.Row="0" ItemsSource="{Binding Model,Converter={StaticResource PropConverter}, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" Grid.IsSharedSizeScope="True"
HorizontalContentAlignment="Stretch"
ItemTemplateSelector="{StaticResource templateSelector}"
/>
and my converter and view model
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string EnumString;
try
{
EnumString = Enum.GetName((value.GetType()), value);
return EnumString;
}
catch
{
return string.Empty;
}
}
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
return null;
//What to do here
}
View model
public class PropertyValue
{
private PropertyInfo propertyInfo;
private object baseObject;
public PropertyValue(PropertyInfo propertyInfo, object baseObject)
{
this.propertyInfo = propertyInfo;
this.baseObject = baseObject;
}
public string Name
{
get { return propertyInfo.Name; }
}
public Type PropertyType { get { return propertyInfo.PropertyType; } }
public object Value
{
get { return propertyInfo.GetValue(baseObject, null); }
set
{
propertyInfo.SetValue(baseObject, value , null);
}
}
}
Try
return (targetType)Enum.Parse(typeof(targetType), value.ToString());
In ConvertBack, is targetType the correct enum type?
If so, I think this should work:
Enum.Parse(targetType, (String)value)
you shoud do it like this
enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };
string colorName = "Blue";
if (Enum.IsDefined(typeof(Colors), colorName)) //true
{
Colors clr = (Colors)Enum.Parse(typeof(Colors), colorName);
}
colorName = "Orange";
if (Enum.IsDefined(typeof(Colors), colorName)) //false
{
Colors clr = (Colors)Enum.Parse(typeof(Colors), colorName);
}
I dont think its feasible to do this with converter or with multibinding.I solved my problem by not using the converter for combobox and checking value in getter and setter.
I modified my class which holds the property info and it works now,I am putting a special check for enum type ,rest of the types I can use converters.
public class PropertyValue
{
private PropertyInfo propertyInfo;
private object baseObject;
public PropertyValue(PropertyInfo propertyInfo, object baseObject)
{
this.propertyInfo = propertyInfo;
this.baseObject = baseObject;
}
public string Name
{
get { return propertyInfo.Name; }
}
public Type PropertyType
{
get { return propertyInfo.PropertyType; }
}
public object Value
{
get
{
var retVal = propertyInfo.GetValue(baseObject, null);
if (PropertyType.IsEnum)
{
retVal = retVal.ToString();
}
return retVal;
}
set
{
if (PropertyType.IsEnum)
{
value = Enum.Parse(propertyInfo.PropertyType, value.ToString());
}
propertyInfo.SetValue(baseObject, value, null);
}
}
}
I dont like corrupting view model for that but I cant see any option at this moment.Please let me know if there are any risks in my code or if you have a better approach.
What I could think quickly is that you can create one class EnumDescriptor with just two properties: EnumString (string) and EnumType (Type)
Your "EnumToListConverter" can return list of EnumDescriptor and you can bind display value of combobox to EnumString property.
In your "EnumToStringConverter", in Convert() you can create instance of EnumDescriptor as we have type and name of enum there, in ConvertBack() you will get the instance of EnumDescriptor from where you can parse the enum value and return it.
Thanks

Categories

Resources