将文本块绑定到WPF中的列表框 [英] binding textblocks to listbox in wpf

查看:70
本文介绍了将文本块绑定到WPF中的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,身体...
我想学习如何将两个TextBlocks绑定到ListBox上,作为wpf中的源,我的ListBox显示一个人的列表,两个TextBlocks中的一个显示男性人数,另一个显示女性人数而没有任何代码隐藏编码(我想将我的TextBlocks直接用xaml代码绑定到ListBox).
非常感谢亲爱的....!

Hello very body…
I want to learn how can i bind two TextBlocks to a ListBox as source in wpf that my ListBox shows a list of persons and one of two TextBlocks shows count of Male persons and another one shows count of Female persons without any codebehind coding (I want to bind my TextBlocks to ListBox directly in xaml code).
Thanks a lot my dears….!

推荐答案

最好的选择是创建一个ValueConverter(或两个值转换器).您没有提供足够的详细信息来提供正确的答案,但是在这里,我返回一个ListBox绑定到IEnumerable< string>的ItemsSource的位置的计数,并计算该字符串与参数相同的匹配项:

The best option is to create a ValueConverter (or two value converters). You did not give enough detail to provide exactly the right answer, but here I am returning a count of where a ListBox is bound to an ItemsSource of IEnumerable<string>, and counting matches where the string is the same as the parameter:

public class CountConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    var listBox = value as ListBox;
    if (listBox != null)
    {
      var list = listBox.ItemsSource as IEnumerable<string>;
      if (list != null)
        return list.Count(i => i == parameter.ToString());
    }
    return null;
  }

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


这篇关于将文本块绑定到WPF中的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆