【问题标题】:Binding Stackpanel in DataTemplate在 DataTemplate 中绑定 Stackpanel
【发布时间】:2023-03-15 02:24:01
【问题描述】:

我有以下用于列表框的 DataTemplate:

<DataTemplate x:Key="MoviesTemplate">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding S_Poster}" Height="73" Width="73" VerticalAlignment="Top" Margin="0,10,8,0"/>
                <StackPanel Orientation="Vertical" Width="200">
                    <TextBlock Text="{Binding Name}" FontSize="20"/>
                    <TextBlock Text="{Binding Type}" FontSize="16"/>
                    <TextBlock Text="{Binding Theaters_S}" FontSize="16"/>
                </StackPanel>
                <StackPanel ItemsSource="To be bound to the custom stack"/>
            </StackPanel>
        </DataTemplate> 

我需要将堆栈 "StackPanel ItemsSource="To be bound to the custom stack" 绑定到我在名为 "Stack_PIC" 的 C# 代码中定义的堆栈面板一堆图片,编码如下:

for (int i=0; i < full; i++)
{
   Image star = new Image();
   star.Source = new BitmapImage(new Uri("/Images/Small_F.png", UriKind.Relative));
   d.Rating_PIC.Children.Add(star);
}  

我这样做的目的是将rating stars添加到ListBoxItems,那么将这个堆栈的内容绑定到以下自定义内容的正确方法是什么?

更新:

我知道堆栈面板不能以这种方式绑定,替代方法是使用名为 ItemsControl 的结构,如Question 所示。但是,我不明白那里的完整答案以及如何将其映射到我的案例中,因此感谢任何帮助澄清这一点。

【问题讨论】:

    标签: xml windows-phone-7


    【解决方案1】:

    您不能在 StackPanel 上定义 ItemsSource。因此,您应该尝试使用 ListBox 或其他一些 ItemsControl 对象来解决您的问题。

    这个post 应该会让你上路。

    另外,也许考虑使用预制的星标控件(我认为您正在尝试这样做,对吗?),看看this one。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 2017-08-26
      • 2011-05-11
      • 2017-02-18
      • 2021-12-17
      • 2023-04-05
      相关资源
      最近更新 更多