【发布时间】: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