【问题标题】:How can I get the following value of the property in a TextBlock which has its source as Data Binding in a Windows Phone application如何在 TextBlock 中获取以下属性值,该 TextBlock 的源为 Windows Phone 应用程序中的数据绑定
【发布时间】:2014-10-20 16:19:38
【问题描述】:

请看一下这张图片以了解我的情况

所以我在我的 Windows Phone 应用程序中有上述场景,其中我有一个具有以下布局的 ListBox。

我的列表框 XAML

                    <ListBox x:Name="llsIceCreamBrands" Margin="0,54,0,0" CacheMode="BitmapCache">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,40">
                                <StackPanel>
                                    <Border BorderBrush="{StaticResource PhoneProgressBarBackgroundBrush}" BorderThickness="3">
                                        <Image Width="200" Height="200" Source="{Binding IceCreamBrandImage }" Margin="3,0,0,0" Stretch="Fill"/>
                                    </Border>
                                </StackPanel>
                                <StackPanel>
                                    <Grid Margin="20,-5,0,0" Height="250" >
                                        <StackPanel>
                                            <TextBlock Text="{Binding IceCreamBrandName }" FontWeight="SemiBold" FontSize="34" FontFamily="Segoe WP" Margin="0" TextWrapping="Wrap" Width="184" VerticalAlignment="Top" HorizontalAlignment="Left" Opacity="1" LineStackingStrategy="BlockLineHeight" LineHeight="35" Height="Auto" />
                                            <TextBlock Text="{Binding IceCreamBrandFlavour }" FontWeight="Normal" FontSize="15" FontFamily="Segoe WP" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" Opacity="0.7" HorizontalAlignment="Left" IsHitTestVisible="False" Height="140"/>
                                        </StackPanel>
                                    </Grid>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

在 C# 代码中:

void GetIceCreamBrands()
    {
        string mystring = "";
        for (int i = 0; i < dc.IceCreams[cID].Brands.Count; i++)
        {
            var bmp = new BitmapImage();

            if (dc.IceCreams[cID].Brands[i].HasArt)
            {
                bmp.SetSource(dc.IceCreams[cID].Brands[i].GetImage());
            }
            else
            {
                bmp.CreateOptions = BitmapCreateOptions.None;
                bmp.UriSource = new Uri("/Assets/Images/IceCreams/Placeholder.png", UriKind.Relative);
            }

            for (int j = 0; j < dc.IceCreams[cID].Brands.Count; j++)
            {

                for (int k = 0; k < dc.IceCreams[cID].Brands[j].Flavour.Count; k++)
                {
                    sourceFlavourList.Add(new Flavour
                    {
                        FlavourBrand = dc.IceCreams[cID].Brands[j].Name,
                        BrandFlavourName = dc.IceCreams[cID].Brands[j].Flavour[k].Name
                    });

                }

            }

            sourceIceCreamBrands.Add(new IceCreamBrands
            {
                IceCreamBrandName = dc.IceCreams[cID].Brands[i].Name,
                IceCreamBrandImage = bmp,
                IceCreamBrandFlavour = "Get Flavours here for the particular brand somehow?"
            });

        }

        llsIceCreamBrands.ItemsSource = sourceIceCreamBrands.ToList();
    }

我的代码中有上述方法可以成功获取品牌及其图片,但口味也是Collection,我尝试使用

将其放入单个字符串中
string mystring = string.Join(Environment.NewLine, sourceFlavourList.Select(x => x.IceCreamBrandFlavourName));

但这对于 Ben&Jerrys 和 Haagen-Dazs 的返回值相同 - 这是两个品牌口味的融合。

我怎样才能实现我正在寻找的东西?

【问题讨论】:

    标签: c# list xaml binding nested


    【解决方案1】:

    我没有看到您在 GetIceCreamBrands() 中定义变量“sourceFlavourList”的位置,所以我假设您在代码的其他地方已经定义了这个变量。这可以解释为什么当您不断将所有口味添加到这个变量时,您会看到完整的口味列表(来自两个品牌)。

    因此,您需要绑定到每个品牌列表(您的结果中确实有),而不是使用这个完整的列表变量。所以您可以使用相同的 linq,但使用不同的源变量) .

    试试这样的:

    var flavours = string.Join(Environment.NewLine, dc.IceCreams[cID].Brands[j].Flavour.Select(x => x.Name));
        sourceIceCreamBrands.Add(new IceCreamBrands
        {
             IceCreamBrandName = dc.IceCreams[cID].Brands[i].Name,
             IceCreamBrandImage = bmp,
             IceCreamBrandFlavour = flavours
        });
    

    【讨论】:

    • 您好,感谢您的回复,但这次它将最后一个品牌的口味返回到所有品牌:(
    • 你能在 GetIceCreamBrands() 的末尾检查 sourceIceCreamBrands 的结果吗?是你所期望的吗(两个项目,每个都有正确的图像和口味列表)?如果是这样,你是否正确绑定到这个变量(sourceIceCreamBrands)?
    • 是的,我得到了正确的图像和正确的品牌名称,但我想我刚刚能够弄清楚...
    • 我刚刚弄明白了!我创建了一个新的List &lt;string&gt;(),并在每次j++ 发生时和i++ 发生时将var flavours 添加到列表中-我通过将[i] 指定为List &lt;string&gt; 的索引来获得风味:)跨度>
    • 非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多