【问题标题】:IValueConverter data bindingIValueConverter 数据绑定
【发布时间】:2011-01-12 10:22:13
【问题描述】:

我正在自学一些 .NET 编程,目前正在尝试在 WPF 中构建标签云控件。目的是在一个窗口上有两个列表框,第一个列表框显示“联系人列表”列表,第二个列表框显示与联系人列表关联的“标签”(或标签)。对于标签,目的是使用 IValueConverter 将字体大小绑定到 itemCount,因此如果我有一个特定标签在我的集合中出现多次,它将在标签列表框中以更大的字体显示。此外,我正在从 DB2 数据库中填充我的控件。

所以我已经在正确的列表框中显示了联系人列表和标签,我只是在绑定方面遇到了一些问题。我正在使用从教程中获取的转换器类,并且想知道是否有人可以帮助我完成这项工作。非常感谢 - 本

属性

public class Label
{
    public int LabelID { get; set; }
    public string LabelName { get; set; }

}

联系人列表类

public class ContactList    
    {           
        public string ContactListName { get; set; }
        public List<Label> Labels { get; set; }

    }  

转换器

   public class CountToFontSizeConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, 
            object parameter, CultureInfo culture)
        {
            const int minFontSize = 6;
            const int maxFontSize = 38;
            const int increment = 3;
            int count = (int)value;
            return ((minFontSize + count + increment) < maxFontSize) ? 
                    (minFontSize + count + increment)                : 
                    maxFontSize;
        }

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

窗口加载事件

private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        //TODO: Add event handler implementation here.
        ListCollectionView lcv = new ListCollectionView(myLabels);

        lcv.GroupDescriptions.Add(new PropertyGroupDescription("LabelName"));

        tagsList.ItemsSource = lcv.Groups;                    
    }

XAML

<Window.Resources>
    <local:CountToFontSizeConverter x:Key="CountToFontSizeConverter"/>

    <Style x:Key="tagsStyle" TargetType="{x:Type ListBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBox}">
                    <Grid>
                        <Border x:Name="Border" 
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"/>
                            <WrapPanel Orientation="Horizontal" 
                                       Margin="2" 
                                       IsItemsHost="true" 
                                       Background="#FFFCF6F6"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <DataTemplate x:Key="ContactsTemplate">
    <WrapPanel>
        <TextBlock TextWrapping="Wrap" 
                       Text="{Binding ContactListName, Mode=Default}"/>
    </WrapPanel>
    </DataTemplate>

    <DataTemplate x:Key="TagsTemplate">
    <WrapPanel>
        <TextBlock Text="{Binding LabelName, Mode=Default}" 
                       TextWrapping="Wrap" 
                       FontSize="{Binding ItemCount, 
                                  Converter={StaticResource CountToFontSizeConverter}, 
                                  Mode=Default}" 
                       Foreground="#FF0D0AF7"/>
    </WrapPanel>
    </DataTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot" Background="#FFCBD5E6">
    <ListBox x:Name="contactsList" 
             SelectionMode="Multiple" 
             Margin="7,8,0,7" 
             ItemsSource="{Binding ContactLists, Mode=Default}" 
             ItemTemplate="{DynamicResource ContactsTemplate}" 
             HorizontalAlignment="Left" 
             Width="254"/>

    <ListBox x:Name="tagsList" 
             Margin="293,8,8,8" 
             ItemsSource="{Binding Labels, Mode=Default}" 
             ItemTemplate="{StaticResource TagsTemplate}" 
             Style="{StaticResource tagsStyle}" />
</Grid>

【问题讨论】:

  • 仅供参考 - 我已重新格式化您的问题,使其更具可读性。起初,我不知道所有这些东西都在那里。将来,如果您以更易读的格式发布内容,您更有可能获得答案——我(作为一个)通常会跳过只有一大堆代码转储在那里的问题。如果这是您自己代码的复制粘贴,我谦虚地建议您也在那里进行一些格式化。它使维护该代码变得非常容易。

标签: c# wpf data-binding db2 ivalueconverter


【解决方案1】:

你的问题不是很准确,所以我只是吐一些cmets。

问题在于您的模型(Label 和 ContactList)没有实现 INotifyPropertyChanged,因此 WPF 很难知道此类属性发生了什么变化。但是,如果您不在运行时更改这些值,那应该不是问题。

另外,最好返回 Binding.DoNothing 而不是抛出 NotImplementedException。

您也可以从任何地方删除 Mode=Default,这不是必需的。

我看到您正在绑定到包含标签的列表框。但是你为什么不绑定联系人列表呢?您应该全部通过代码设置,或者使用绑定(我更喜欢绑定)。

如果你能提供更多关于实际问题的细节,也许我可以更好地帮助你。

【讨论】:

  • 感谢您的回复,对于含糊不清的问题感到抱歉。所以我不会在运行时更改值,这就是我没有实现 INotifyPropertyChanged 的​​原因。实际的问题是如何将我的 tagsTemplate 中的 LabelName 绑定到该名称在我的集合中出现的次数?
【解决方案2】:

Ben,我仍然不确定您面临的问题;

运行代码时到底发生了什么? 您的 Label 名称的字体大小是否更改?

我可以在您的代码中看到的问题 -

1 您在 XAML 和代码中为您的 tagsList 设置 ItemsSource -

ItemsSource="{绑定标签, 模式=默认}"

tagsList.ItemsSource = lcv.Groups;

2 我没有在任何地方看到定义的 ItemCount 属性?

3 FontSize 是一个双精度值,因此您的转换器应该返回双精度值而不是整数。

4 您的“标签”列表应该是一个可观察的集合(以防在运行时添加或删除项目)。

【讨论】:

    猜你喜欢
    • 2010-09-15
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 2012-04-06
    • 1970-01-01
    • 2015-09-21
    相关资源
    最近更新 更多