Why cannot get image for my listview?

120 Views Asked by At

HI to all I facing one problem which is that my IValueConverter cannot get the new image:

Is my code correct? XAML FILE:

<Window.Background>
        <!--Budget Categories-->
        <DataTemplate x:Key="CategoriesName">
            <Border BorderBrush="#FF000000" BorderThickness="1,1,0,1" Margin="-6,-2,-6,-2">
                <StackPanel Margin="6,2,6,2">
                    <TextBlock Text="{Binding Path=CategoriesName}"/>

                </StackPanel>
            </Border>
        </DataTemplate>

        <DataTemplate x:Key="CategoriesType">

            <Border BorderBrush="#FF000000" BorderThickness="1,1,0,1" Margin="-6,-2,-6,-2">
                <!--<StackPanel Margin="6,2,6,2">
                    <TextBlock Text="{Binding Path=CategoriesType}"/>
                </StackPanel>-->
                <StackPanel Margin="6,2,6,2">
                    <Image Source="{Binding Path=CategoriesType,Converter={StaticResource typeToImageConverter}}" Width="16" Height="16" Margin="3,0"/>
                    <TextBlock Text="{Binding Path=CategoriesType}"/>
                </StackPanel>
            </Border>
        </DataTemplate>



        <DataTemplate x:Key="CategoriesExpect">
            <Border BorderBrush="#FF000000" BorderThickness="1,1,0,1" Margin="-6,-2,-6,-2">
                <StackPanel Margin="6,2,6,2">
                    <TextBlock Text="{Binding Path=CategoriesTotalExpect}"/>
                </StackPanel>
            </Border>
        </DataTemplate>

        <DataTemplate x:Key="CategoriesActual">
            <Border BorderBrush="#FF000000" BorderThickness="1,1,1,1" Margin="-6,-2,-6,-2">
                <StackPanel Margin="6,2,6,2">
                    <TextBlock Text="{Binding Path=CategoriesTotalActual}"/>
                </StackPanel>
            </Border>
        </DataTemplate>
        <!--Budget Categories-->
</Window.Background>

   <ListView Height="320" HorizontalAlignment="Left" Margin="12,154,0,0" Name="CategoriesListView" VerticalAlignment="Top" Width="316" ItemsSource="{Binding}" ItemContainerStyle="{DynamicResource MyItemContainerStyle}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Categories" Width="150" CellTemplate="{DynamicResource CategoriesName}" />
                    <GridViewColumn Header="Types" Width="40"  CellTemplate="{DynamicResource CategoriesType}"/>
                    <GridViewColumn Header="Expect" Width="60"  CellTemplate="{DynamicResource CategoriesExpect}"/>
                    <GridViewColumn Header="Actual" Width="60"  CellTemplate="{DynamicResource CategoriesActual}"/>
                </GridView>
            </ListView.View>
        </ListView>

This is my TypeToImageConverter.cs:

public class TypeToImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {

string str = (string)value;

if (str == "income")
   return new BitmapImage(new Uri("pack://application:,,,/images/add.png"));

if (str == "Expenses")
{
    return new BitmapImage(new Uri(@"pack://application:,,,/HouseWivesSavior;component/images/add.png"));
}

    return null;
}

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
1

There are 1 best solutions below

0
On

I found the solution for my answer which is the format of the string. Which causing me to unavailable to retrieve the image with few hours of troubleshooting & debugging. I finally found out the solution :)

I solve it by like this:In my converter.cs

  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{

    string str = (string)value;
    string newString = str.TrimEnd();//Newly added compared with the old version

    if (newString == "income")
       return new BitmapImage(new Uri("pack://application:,,,/images/add.png"));

    if (newString == "Expenses")
    {
        //return new BitmapImage(new Uri(@"pack://application:,,,/HouseWivesSavior;component/images/add.png"));
        return new BitmapImage(new Uri("pack://application:,,,/images/edit.png"));
    }

    return null;
} 

From referring the above that you can see that I added this code:
"string newString = str.TrimEnd();"

Is because I don't want extra white space at the end of the string. As during insert into the database that my code is look like this:

if (IncomeButton.IsChecked == true) {
                CategoryType = IncomeButton.Content.ToString();
            }else{
                CategoryType = ExpensesButton.Content.ToString();
            }

During runtime, I found out that why the value look strange in the format of "Expenses " instead of "Expenses"... Therefore I tried with trim of the end part see how & Bingo. I got it working like a charm.

I refered this video to out that how to trace the value: http://www.youtube.com/watch?v=evO3_xutDYI

Thank you all guys for answering my question & sorry for wasting your time & effort to solve my question :) And special thank to #Blam which answered me :) Good luck to all of you & have a nice day.