Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

I've been trieng for hours and cant find the answer on the internet.. i'm about to give up..

xaml:

XML
<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:

C#
if (hulpBlock.Text == "Partly Cloudy")
             { weatherframe.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("WeatherIcons/03.png"); }
Posted
Updated 1-Mar-12 20:51pm
v5

As I saw, the Text property of your TextBlock is bound to the condition property of your ListBox's item's value.


So, you can check the condition property of your ListBox's SelectedValue instead.


Edited:


You can set a name to your ListBox:


XML
<ListBox x:Name="myListBox" 
    ... >
    ...
</ListBox>

get the SelectedItem (assuming that MyDataType is the type of your item's value):


C#
MyDataType val = myListBox.SelectedValue as MyDataType;

and, use this value in your condition (assuming that condition is a string):


C#
if (val != null)
{
if (val.condition == "Partly Cloudy")
             { weatherframe.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("WeatherIcons/03.png"); }
}
 
Share this answer
 
v3
Comments
young blade 2-Mar-12 5:46am    
Thanx for your reply,

I understand but dont know how to do it.. I even followed your link.. Could you please explain it a little bit more..?
Shmuel Zang 2-Mar-12 7:12am    
See the edited solution.
young blade 2-Mar-12 9:14am    
I'm a litle bit further.. but still stuck see below..
Shmuel Zang 3-Mar-12 13:27pm    
I saw you finally found another solution. It should also work.
Shmuel Zang,

Thank you for your quick answer. My listbox name = listboxc

and so I have:

MyDataType val = listboxc.SelectedValue as MyDataType;


the problem is that I get an error:

Error 1 The type or namespace name 'MyDataType' could not be found (are you missing a using directive or an assembly reference?

Maybe its good to know that "condition" comes from an Class named WeatherInfo.cs

and contains the following:

C#
namespace _7th_Heaven
{
    public class WeatherInfo
    {
        public string day_of_week { get; set; }
        public string low { get; set; }
        public string high { get; set; }
        public string icon { get; set; }
        public string condition { get; set; }
    }



}


To be short, I dont understand what you mean with:

get the SelectedItem (assuming that MyDataType is the type of your item's value):


MyDataType val = myListBox.SelectedValue as MyDataType;


I feel we are close.. but not there yet..
 
Share this answer
 
Comments
Shmuel Zang 3-Mar-12 14:36pm    
So, just replace MyDataType with WeatherInfo.
I got it resolved:

I gave the textblock an "loaded event handeler"

HTML
<textblock loaded="test_Loaded" text="{Binding Path=condition}" grid.column="1" margin="10,75,10,0" x:name="temp" xmlns:x="#unknown"></textblock>


And did this in my xaml.cs:

C#
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 .. :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900