繁体   English   中英

无法访问DataTemplate内部的文本块名称

[英]Can't acces textblock name thats inside DataTemplate

我试图获取由“ condition”返回的值,所以想法是在if语句中使用textblocks名称,以便更改图像的来源。

当我尝试使用datatemplate之外的textblock进行操作时,一切都好..但是当我选择datatemplate内部的textblock时,我得到一个错误,提示textblock不存在。 我需要这样做,因为当天气变化时,我需要另外一张图像。

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

为了更新weatherframe.Source,您必须在TextBlock的Text属性上订阅更改后的事件。 更好的方法是将weatherframe.Source实现为Dependency属性(如果尚未实现),则可以直接使用适当的Value Convertercondition绑定到weatherframe.Source

您的ValueConverter应该看起来类似于以下内容:

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
}

然后在您的XAML中:

在您的资源部分:

<StringToImageConverter x:Key="StringToImageConverter"/>

在您的GUI中:

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

我解决了:

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

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

并在我的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不可能等于“ Party Cloudy”,但是微调器完成了工作.. :-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM