【问题标题】:TreeView item foreground binding MVVMTreeView 项目前台绑定 MVVM
【发布时间】:2013-09-09 05:12:05
【问题描述】:

我正在使用 TreeView 来显示我的数据。我想绑定树视图项的前景。下面是我的代码。

查看:

<UserControl.Resources>
    <HierarchicalDataTemplate x:Key="TreeViewItem" ItemsSource="{Binding Children}">
        <StackPanel Orientation="Horizontal">
            <CheckBox Margin="2" IsChecked="{Binding IsChecked, Mode=TwoWay}" Content="{Binding Title}" 
                        Background="{Binding Path=ForegroundColor}" IsThreeState="True"/>
        </StackPanel>
    </HierarchicalDataTemplate>
</UserControl.Resources>
<Grid>
    <TreeView Margin="5, 0, 5, 0" ItemsSource="{Binding GeoGraphixModules}" ItemTemplate="{StaticResource TreeViewItem}" IsEnabled="{Binding TreeViewEnabled}" />
</Grid>

在我的视图模型中

公共类 SomeTreeViewItem { 公共收藏儿童 { 得到{返回_children; } }

public Brush ForegroundColor
{
    get
    {
        if (SomeCheck)
            return Brushes.Green;
        else
            return Brushes.Red;
    }
}    

}

现在,当我调试此应用程序“ForegroundColor”时,该应用程序仍显示黑色作为所有子项的前景。这里有什么问题?

【问题讨论】:

  • 您好 Faisal,如果您调试此应用程序,您的输出窗口中是否存在绑定表达式错误?
  • System.Windows.Data 错误:1:无法创建默认转换器以在“System.Drawing.Brush”和“System.Windows.Media.Brush”类型之间执行“单向”转换。考虑使用 Binding 的 Converter 属性。绑定表达式:路径=前景颜色; DataItem='GeoGraphixModule' (HashCode=63453159);目标元素是'CheckBox'(名称='');目标属性是“背景”(类型“画笔”)

标签: c# mvvm treeview treeviewitem


【解决方案1】:

在尝试创建相同的错误后,我创建了以下视图模型

public class ViewModel
{
    private Collection<GeoGraphixModule> _geoGraphixModules;

    public ViewModel()
    {
        _geoGraphixModules = new Collection<GeoGraphixModule>();
        _geoGraphixModules.Add(new GeoGraphixModule(){Children = new Collection<Bla>(){new Bla{Children = new Collection<Bla>{new Bla(), new Bla(), new Bla()}}}});
        _geoGraphixModules.Add(new GeoGraphixModule(){Children = new Collection<Bla>(){new Bla{Children = new Collection<Bla>{new Bla(), new Bla(), new Bla()}}}});
        _geoGraphixModules.Add(new GeoGraphixModule(){Children = new Collection<Bla>(){new Bla{Children = new Collection<Bla>{new Bla(), new Bla(), new Bla()}}}});
        _geoGraphixModules.Add(new GeoGraphixModule(){Children = new Collection<Bla>(){new Bla{Children = new Collection<Bla>{new Bla(), new Bla(), new Bla()}}}});
    }

    public Collection<GeoGraphixModule> GeoGraphixModules
    {
        get { return _geoGraphixModules; }
        set { _geoGraphixModules = value; }
    }
}

public class GeoGraphixModule
{
    public Brush ForegroundColor
    {
        get
        {
            if (SomeCheck())
                return Brushes.Green;
            return Brushes.Red;
        }
    }

    private bool SomeCheck()
    {
        Random random = new Random();
        int randomNumber = random.Next(0, 100);

        if ((randomNumber % 2) == 0)
            return true;
        return false;
    }

    private Collection<Bla> _children;
    public Collection<Bla> Children { get; set; }
}

public class Bla
{
    private bool? _isChecked;
    private string _title;
    private Collection<Bla> _children;

    public bool? IsChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;
        }
    }

    public Collection<Bla> Children
    {
        get { return _children; }
        set { _children = value; }
    }

    public string Title
    {
        get { return _title; }
        set
        {
            _title = value;
        }
    }
}

我知道它非常丑陋,但它适用于顶级在下面的图像中掠夺

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    相关资源
    最近更新 更多