1

我正在使用 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 { public Collection Children { get { return _children; } }

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

}

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

4

1 回答 1

0

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

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

我知道它非常难看,但它适用于顶层,在下面的图片中掠夺

在此处输入图像描述

于 2013-09-09T06:00:14.703 回答