微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将值传递给 iValueConverter

如何解决如何将值传递给 iValueConverter

我试图解决以下问题(最后成功了,但可能不是最好的方式)。这是我第一次尝试的方式:

我正在显示一个带有目录的树视图和一个带有此 WPF 代码的复选框:

<Window.DataContext>
    <local:viewmodel/>
</Window.DataContext>

<Grid>
    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
        <StackPanel.Resources>
            <!-- This Style is applied to all TextBlock elements in the command strip area. -->
            <Style targettype="TextBlock">
                <Setter Property="VerticalAlignment" Value="Center" />
                <Setter Property="Foreground" Value="#EE000000" />
            </Style>
            <local:ColorConverter x:Key="XcolorConverter" />
        </StackPanel.Resources>
        <TreeView ItemsSource="{Binding View}">
        <TreeView.Resources>
                    <HierarchicalDataTemplate DataType="{x:Type local:Folder}" ItemsSource="{Binding SubFolders}">
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <TextBlock Background="{Binding Path=.,Converter={StaticResource XcolorConverter}}" Text="{Binding Name}"/>                            
                        <CheckBox Focusable="False" IsChecked="{Binding IsChecked}"  VerticalAlignment="Center"/>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
    </TreeView>
    </StackPanel>
</Grid>

在下面的 ColorConverter 方法 Convert 中,我需要知道的是满足特定标准的颜色目录的完整目录名称。参数“value”是一个带有值 (MyNameSpace).Folder 的字符串。如果我在调试器中检查“值”,我还会看到“名称”,它是 Treeview 文本框中显示的目录名称(没有前面的完整路径)。但是,我无法在程序中访问 value:Name(错误 CS1061:'object' 不包含 'Name' 的定义,我不明白为什么我可以在调试器中看到它但不能访问它)也不会帮助我,因为我需要完整的目录路径。在 viewmodel 类/代码中,有一个 ForEach 将目录名称分配给 ObservableCollection 文件夹。对象参数为空;我知道我可以在 xaml 中添加 ConverterParameter=,但不知道如何从该 xaml 中访问实际显示的目录。

我应该如何更改 WPF 以便我的 colorConverter.Convert 方法可以访问它当时显示的(完整)目录?

    public ICollectionView View { get => cvs.View; }
    private CollectionViewSource cvs = new CollectionViewSource();
    private ObservableCollection<Folder> col = new ObservableCollection<Folder>();

公共类文件夹 { 公共字符串名称{获取;放; } 公共 ObservableCollection 子文件夹 { get;放; } = new ObservableCollection(); }

public partial class ColorConverter : IValueConverter
    {
        private static int count;
        public object Convert(object value,Type targettype,object parameter,System.Globalization.CultureInfo culture)
        { // Set color based upon directory,something like if paramater.(directory=c:\\temp")...
            return Brushes.Green;
        }
     }   

解决方法

如果我理解正确的话:

public object Convert(object value,Type targetType,object parameter,CultureInfo culture)
{
    // Validating the type of incoming value
    if (value is Folder folder)
    {
        // Here we work with the folder variable
        string name = folder.Name;

        // Set color based upon directory,something like if paramater.(directory=c:\\temp")...
        return Brushes.Green;
    }
    // If the received value is not of the Folder type,// then the converter returns an undefined value.
    return DependencyProperty.UnsetValue;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?