【问题标题】:Binding to SelectedItems.Count in Windows Store App在 Windows 应用商店应用中绑定到 SelectedItems.Count
【发布时间】:2012-09-04 11:39:03
【问题描述】:

我想将我的 TextBlock.Text 绑定到 ListBox.SelectedItems.Count,但我发现当我在我的 listBox 中多选项目时,我的 TextBlock 不显示任何内容。

我记得这种方式在 WPF 中有效,但在 Windows Store App 中不再有效。

有没有其他方法可以解决这个简单的问题?

   <StackPanel>
        <StackPanel.Resources>
            <local:NumberToTextConverter x:Key="NumToText" />
        </StackPanel.Resources>
        <TextBlock Text="{Binding SelectedItems.Count, ElementName=listBox, Mode=TwoWay, Converter={StaticResource NumToText}}"
                   Height="80" />
        <ListBox x:Name="listBox"
                 SelectionMode="Multiple" />
    </StackPanel>

这是转换器,但在这种情况下不是必需的。

internal class NumberToTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string culture)
    {
        if(value != null)
            return ((int)value).ToString();

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string culture)
    {
        throw new NotImplementedException();
    }

}

我在程序的入口处加载了一些数据。

        List<string> gogoString = new List<string>();

        for (int i = 0; i < 4; i++)
            gogoString.Add(i.ToString());

        listBox.ItemsSource = gogoString;

【问题讨论】:

  • 您是否尝试从绑定中删除Mode="TwoWay
  • 是的,但是它也不起作用。也许微软 Metro api 团队将来会改进这一点?
  • 如果在转换器中放置断点,“value”的值是多少?
  • @mathieu,我试过了。该程序无法访问我的转换器。我还在 msdn Metro 论坛上发布了该主题。目前只是我知道处理问题的一种正常方式。见social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/…

标签: c# xaml windows-store-apps


【解决方案1】:

好吧,我迟到了。

ListBox 在 WPF 中实现 INotifyPropertyChanged,但在 Windows 应用商店应用中没有。这就是为什么我无法在 Windows 应用商店应用中收到来自 ListBox.Items.CountListBox.SelectedItems.Count 的通知。

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2023-03-14
    相关资源
    最近更新 更多