简体   繁体   English

WPF ListView中的转换器,使用缓存的值作为参数

[英]Converter in WPF ListView using cached value as parameter


I have an application that manages documents of different type. 我有一个管理不同类型文档的应用程序。 In a document manager I diaplay the documents in a ListView. 在文档管理器中,我在ListView中显示文档。 I have created some converter to diaplay special aspacts of a document without messing with the document class itself. 我创建了一些转换器,以在不弄乱文档类本身的情况下显示文档的特殊aspacts。 One column in the list view shall display an icon that represent the document type and a document number. 列表视图中的一列应显示一个代表文档类型和文档编号的图标。 The columns shall look like 列应如下所示

 <icon> D 1 <icon> D 11 ... 

The document number shall be padded left to fit the biggest number. 文件编号应在左侧填充以适合最大编号。

Currently I have a converter which creates the desired term 目前,我有一个转换器,可以创建所需的字词

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
D{
    System.Windows.Controls.ListView view = (System.Windows.Controls.ListView)parameter;
    int count = (view.Items.Count.ToString().Count());
    string id = "D" + value.ToString().PadLeft(count);
    return id;
}

The converter is used in a CellTemplate where the converter parameter is listview itself 转换器用于CellTemplate,其中转换器参数是listview本身

<TextBlock Text="{Binding Number, Converter={StaticResource docIDConverter}, ConverterParameter={x:Reference Documents}}" />

I know the amount of documents in a project and can provide it as propperty of my window class 我知道项目中的文档数量,可以将其作为窗口类的属性来提供

    /// <summary>
    /// The amount of documents contained in a project
    /// </summary>
    int documentCount = 0;
    public int DocumentCount {
        get { 
            if(this.documentCount == 0)
                documentCount = Project.Documents.Count;
            return documentCount;
        }
    }

This works but I consider this as bad style and it may be slow on big lists. 这行得通,但我认为这是不好的风格,在大名单上可能会很慢。

How can I use the propperty DocumentCount as parameter to the converter. 如何使用属性DocumentCount作为转换器的参数。 Or How can I use the propperty DocumentCount as binding in a multi value converter Thanks Clemens Hoffmann 或者如何在多值转换器中将属性DocumentCount用作绑定对象感谢Clemens Hoffmann

ConverterParameters are not dependency properties. ConverterParameters不是依赖项属性。 Thus, you can't bind to them. 因此,您无法绑定到它们。 But you can use multi-binding to get the same effect: 但是您可以使用多重绑定获得相同的效果:

<Style TargetType="FrameworkElement">
    <Setter Property="Text">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource docIDConverter}">
                <Binding Path="DocumentCount" RelativeSource="RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}"/>
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Style>

The multi-value converter now gets an array of source values as input: 现在,多值转换器将源值数组作为输入:

public class AccessLevelToVisibilityConverter : IMultiValueConverter {
    public object Convert(
            object[] values, Type targetType, object parameter, CultureInfo culture)
       {
            int count = values.All(v => (v is int);
            string id = "D" + values.All(v => (v is TypeYouAreExpectingHere).ToString().PadLeft(count);
            return id;
        }

        public object[] ConvertBack(
            object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM