【问题标题】:How to pass a value to iValueConverter如何将值传递给 iValueConverter
【发布时间】:2021-08-14 16:35:29
【问题描述】:

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

我正在显示一个带有目录的树视图和一个带有此 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 文本框中显示的目录名称(没有前面的完整路径)。但是,我无法在程序中访问值:名称(错误 CS1061:“对象”不包含“名称”的定义,我不明白为什么我可以在调试器中看到它但无法访问它)也不会帮助我,因为我需要完整的目录路径。在 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 子文件夹 { 获取;放; } = 新的 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;
        }
     }   

【问题讨论】:

  • 这些都可能对您有所帮助:System.IO.Path.GetDirectoryName()/GetExtension()/GetFileName()/GetFileNameWithoutExtension()/GetFullPath()
  • 通常我会推荐使用 valuconverter stat 检查 strign 是否包含子字符串。子字符串将通过转换器参数发送。然后添加大致像这样的触发器 Textbox-triggers-text-stringcontainssubstring("substring")-value true-background-hotpink
  • 谢谢丹尼斯,但我知道如何获取目录但我仍然不知道如何在转换器参数中获取它,在阅读您的第二条评论后也不知道。
  • 这能回答你的问题吗? 'object' does not contain a definition
  • “我不明白为什么我可以在调试器中看到它但不能访问它”——因为调试器知道你没有告诉转换器中的实际代码。您需要 cast 传递给您的方法的 object 参数,以便编译器知道对象实际是什么类型。请参阅建议的副本。

标签: wpf treeview viewmodel ivalueconverter


【解决方案1】:

如果我理解正确的话:

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;
}

【讨论】:

  • 谢谢大家的回复!我还添加了一个 NameFull,它在 ViewModel 中分配了 GetFullPath,当在 Convert 方法中使用 -string name=folder.NameFull- 时,我可以访问完整目录。当我确实在调试器中看到 Name 值时,我无法弄清楚如何获取所需的信息。正如你的代码和彼得斯所说的那样,我必须做的就是铸造。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-24
  • 1970-01-01
  • 2018-06-06
  • 2021-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多