I have the Contact class:
public class Contact
{
public Contact(Contact contact)
{
this.Username = contact.Username;
this.GUID = contact.GUID;
this.Msg = contact.Msg;
this.Ring = contact.Ring;
}
public string Username { get; set; }
public Guid GUID { get; set; }
public bool Msg { get; set; }
public bool Ring { get; set; }
}
This is the xaml:
<ListView Grid.Row="1" Grid.Column="0" Name="ContactsListView"
IsItemClickEnabled="True"
ItemsSource="{x:Bind m_Client.Contacts}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Contact">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Bind Username}" VerticalAlignment="Center" />
// HERE SHOULD BE THE <Image> THAT SHOULD BE BOUND TO THE Msg PROPERTY
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
What I need to accomplish is that when the Msg boolean is true, the image source to be one image, and when the Msg boolean is false the image source to change to the second image.
EDIT: I created this class:
namespace ContactsListBinding.Models
{
public class MyImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return (bool)value ? new BitmapImage(new Uri("ms-appx:///Assets/true.png")) : new BitmapImage(new Uri("ms-appx:///Assets/false.png"));
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}
This is the XAML:
<Page
x:Class="ContactsListBinding.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ContactsListBinding"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:data="using:ContactsListBinding.Models"
xmlns:namespace="ContactsListBinding.Models">
<Page.Resources>
<data:MyImageConverter x:Key="MyImageConverter" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.Column="0" Name="AddContactButton" Content="Add Contact" Click="AddContactButton_Click" />
<ListView Grid.Row="1" Grid.Column="0" Name="ContactsListView"
IsItemClickEnabled="True"
ItemsSource="{x:Bind m_Client.Contacts}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Contact">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{x:Bind Username}" VerticalAlignment="Center" />
<Image Source="{x:Bind Msg, Converter={StaticResources MyImageConverter}}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
What am I doing wrong here?
Create a converter:
public class MyImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return (bool)value ? new BitmapImage(new Uri("trueImagePath")) : new BitmapImage(new Uri("falseImagePath"));
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
You also need to add resource to your page:
<Page.Resources>
<namespace:MyImageConverter x:Key="MyImageConverter" />
</Page.Resources>
And than add image control like this:
<Image Source="{x:Bind Msg, Converter="{StaticResources MyImageConverter}"}" />
I did this
<Page.Resources> <ContactsListBinding.Models:Converter x:Key="Converter" /> </Page.Resources>
and it says Converter is not supported in Windows Universal Project.I just maybe use some internal name, try to rename for ex. to MyImageConverter. EDIT And also namespace must be added in Page element like xmlns:namespace="yourNamespace" or if it's the same namespace as your View, use local:MyImageConverter
Stefan, I hope you put correct image path instead -
trueImagePath
andfalseImagePath
Of course I did :) Check my updated question, there is the new code.