我正在使用TreeView来显示我的数据。我想绑定(bind)树 View 项目的前景。这是下面的代码。

看法:

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

在我看来

公共(public)类SomeTreeViewItem
{
公共(public)收集 child
{
得到{return _children; }
}
public Brush ForegroundColor
{
    get
    {
        if (SomeCheck)
            return Brushes.Green;
        else
            return Brushes.Red;
    }
}

}

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

最佳答案

在尝试创建相同的错误后,我做了以下viewmodel

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

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

关于c# - TreeView项目前景绑定(bind)MVVM,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18691824/

10-12 07:39