Can't acces textblock name thats inside DataTemplate

516 views Asked by At

I'm trying to obtain the value that is returned by "condition" so the idee is to use the textblocks name in an if statement so I can change the source of an image.

when I try to do it with an textblock thats outside of the datatemplate all goes wel.. but as soon as I choose an textblock thats inside the datatemplate I get an error saying that the textblock doesnt exist. I need to do it cause when the weather changes I need another image to go with it.

xaml:

                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Height="99" >
                                    <Grid Height="100">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>                                            
                                         <TextBlock Text="{Binding Path=condition}" Grid.Column="1" Margin="10,75,10,0" Name="hulpBlock"></TextBlock>
                                    </Grid>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>

xaml.cs:

    if (hulpBlock.Text == "Partly Cloudy")
         { weatherframe.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("WeatherIcons/03.png"); }                        
2

There are 2 answers

8
Bojin Li On

In order for your weatherframe.Source to be updated, you would have to subscribe to the changed event on the Text Property of your TextBlock. A more elegant way to do it is to implement weatherframe.Source as a Dependency Property if it isn't already, then you can just bind condition to weatherframe.Source with the appropriate Value Converter directly.

Your ValueConverter should look something similar to this:

public class StringToImageConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if(value == DependencyProperty.UnsetValue || !(value is string))
        {
            return null; //Handle error your way here
        }
        if ((string)value == "Partly Cloudy")
        {
            return new ImageSourceConverter().ConvertFromString("WeatherIcons/03.png");
        }
        else
        {
           // More Implementations and error handling etc
        }
        return null;
    }


    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }

    #endregion
}

Then in your XAML:

In your resources section:

<StringToImageConverter x:Key="StringToImageConverter"/>

In your GUI:

 <Weatherframe Source="{Binding Path=condition, Converter={StaticResource StringToImageConverter}}" Name="weatherframe"></Weatherframe>
0
young blade On

I got it resolved:

I gave the textblock an "loaded event handeler"

    <TextBlock Loaded="test_Loaded" Text="{Binding Path=condition}"  Grid.Column="1" Margin="10,75,10,0" x:Name="temp" ></TextBlock>

And did this in my xaml.cs:

    private void test_Loaded(object sender, RoutedEventArgs e)
    {
        var hulpBlock = sender as TextBlock;
        if (hulpBlock.Text.Trim().Equals("Partly Cloudy"))
        {
            Weatherframe.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("WeatherIcons/03.png");
        }
    }

my data get pulled in from an internet xml source. and suposibly there is some extra hidden data in the text which makes it impossible for the hulpBlock.Text to equal "Partly Cloudy" but the trimmer did the job .. :-)