无法访问DataTemplate中的文本块名称 [英] Can't acces textblock name thats inside DataTemplate

查看:72
本文介绍了无法访问DataTemplate中的文本块名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取由"condition"返回的值,因此想法是在if语句中使用文本块名称,以便更改图像的来源.

当我尝试使用datatemplate之外的textblock进行操作时,一切都会好起来.
但是,一旦我在数据模板中选择了一个文本块,就会收到一条错误消息,指出该文本块不存在.我需要这样做,因为当天气变化时,我需要再加上一张图像.

我已经呆了好几个小时了,无法在互联网上找到答案..我要放弃了..

xaml:

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:

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







xaml.cs:

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

推荐答案

如我所见,您的TextBlockText属性绑定到了ListBox项目的值.

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

因此,您可以检查ListBox

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

您可以为ListBox设置名称:

<ListBox x:Name="myListBox" 

    ... >
    ...
</ListBox>

获取SelectedItem(假设MyDataType是商品值的类型):

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

MyDataType val = myListBox.SelectedValue as MyDataType;

,然后根据您的条件使用此值(假设conditionstring):

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

if (val != null)
{
if (val.condition == "Partly Cloudy")
             { weatherframe.Source = (ImageSource)new ImageSourceConverter().ConvertFromString("WeatherIcons/03.png"); }
}


Shmuel Zang,

谢谢你们的快速响应.我的列表框名称= listboxc

所以我有:

Shmuel Zang,

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

and so I have:

MyDataType val = listboxc.SelectedValue as MyDataType;



问题是我得到一个错误:

错误1找不到类型或名称空间名称"MyDataType"(您是否缺少using指令或程序集引用?

也许很高兴知道条件"来自名为WeatherInfo.cs的类.
并包含以下内容:



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:

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; }
    }



}



简而言之,我不明白您的意思:

获取SelectedItem(假设MyDataType是项目值的类型):




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..


我解决了:

我给了textblock一个加载的事件处理程序"

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" xmlns:x="#unknown"></textblock>



并在我的xaml.cs中进行了此操作:



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");
            }
        }



我的数据从Internet xml源中获取.并且可能在文本中还有一些额外的隐藏数据,
使得hulpBlock.Text不可能等于部分多云",但修剪器完成了工作.. ::)



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 .. :-)


这篇关于无法访问DataTemplate中的文本块名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆