【问题标题】:Stackpanel/Itemscontrol DatabindingStackpanel/Itemscontrol 数据绑定
【发布时间】:2012-09-27 23:23:02
【问题描述】:

我有这个 XAML:

 <ItemsControl x:Name="recentSearches"
               Margin="0,65,0,0">
               <ItemsControl.ItemsPanel>
                   <ItemsPanelTemplate>
                         <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                         <TextBlock Text="{Binding q}"
                                TextWrapping="Wrap"
                                Foreground="AliceBlue"
                                Padding="2,6,2,2"
                                Margin="12,-6,12,0"
                                FontSize="20" />
                      </DataTemplate>
                 </ItemsControl.ItemTemplate>

还有这段代码:

private void showLatestSearches()
        {
            if (fmn.checkLatestSearchesExtistence())
            {
                List<RecentSearchItem> recent = new List<RecentSearchItem>();
                List<String> l = fmn.readLatestSearches();
                for (int i = 0; i <= l.Count-1; i += 1)
                {
                    RecentSearchItem r = new RecentSearchItem();
                    r.q = l[i];
                    r.generalbg = grau;
                    recent.Add(r);
                }
                recentSearches.DataContext = recent;
            }
        }

名为 fmn 的对象从隔离存储中读取 .txt。 但是为什么这个 StackPanel 什么都没有显示呢?

【问题讨论】:

标签: c# itemscontrol stackpanel


【解决方案1】:

ItemsControl.ItemsSource 必须绑定到一个集合,对于通知,最好是ObservableCollection&lt;T&gt;

您在最后一分钟设置 DataContext,更好的方法是设置 DataContext 到 ViewModel,可以是您创建视图的地方。

public class Form :UserControl
{
  DataContext = new YourViewModel() ;
}

在 XAML 中:

ItemsSource="{Binding SearchesCollection}"

SearchesCollection 将是YourViewModelObservableCollection&lt;string&gt; 类型的属性。每当您向SearchesCollection 添加新项目时,视图都会更新。

This Databinding Tutorial 应该会有所帮助。

【讨论】:

  • 我试过这样做&lt;ItemsControl x:Name="recentSearches" ItemsSource="recent" Margin="0,65,0,0"&gt;,但还是不行。
  • 这没有约束力。在我的回答中:ItemsSource="{Binding SearchesCollection}"
  • 抱歉,我的意思是&lt;ItemsControl x:Name="recentSearches" ItemsSource="{Binding recent}" Margin="0,65,0,0"&gt;,但我还是什么都没看到。难道不能绑定到一个简单的List吗?
  • 你需要一个 ObservableCollection。查看我答案中的链接教程。
  • 我知道如何创建和绑定 ListBox。我制作了没有可观察集合的 ListBoxes,它起作用了。那么,在 StackPanel 中是不可能的吗?
【解决方案2】:

感谢 Lews Therin,我终于将数据绑定到堆栈面板:

<ItemsControl x:Name="recentSearches"
              ItemsSource="{Binding recent}"
              Background="{Binding generalbg}"
              Margin="0,65,0,0" Tap="recentSearches_Tap">
                    <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding q}"
                                               Foreground="{Binding foreground}"
                                               TextWrapping="Wrap"
                                               Padding="2,6,2,2"
                                               Margin="12,-6,12,0"
                                               FontSize="20" />
                     </DataTemplate>
                            </ItemsControl.ItemTemplate>
 </ItemsControl>

以及背后的代码:

private void showLatestSearches()
{
    if (fmn.checkLatestSearchesExtistence())
    {
        List<RecentSearchItem> recent = new List<RecentSearchItem>();
        List<String> l = fmn.readLatestSearches();
        for (int i = 0; i <= l.Count-1; i += 1)
        {
            RecentSearchItem r = new RecentSearchItem();
            r.q = l[i];
            r.generalbg = grau;
            r.foreground = blau;
            recent.Add(r);
        }
        recentSearches.ItemsSource = recent;
    }
}

这可行,但不幸的是,似乎无法确定哪个文本框被点击,何时被点击。

【讨论】:

  • 这不是一个好方法。查看视图模型。你是什​​么意思窃听?
  • 当我点击堆栈面板中的一个项目时,我可以为它构建一个事件处理程序,但似乎无法知道堆栈面板中的哪个项目被点击了。
猜你喜欢
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 2013-06-15
  • 2018-09-30
相关资源
最近更新 更多