0

我正在研究 WPF 应用程序 MVVM 模式,我必须在其中创建 aTreeView和 a之间的某种关系Grid。这个想法是突出显示 id 等于树节点 id 的行。

显示颜色属性

 public Brush DisplayColor
    {
        set
        {
            _displayColor = value;
            NotifyPropertyChanged("DisplayColor");
        }

        get { return _displayColor; }

    }

**Select TreeNode value.id** 



    private MessageElementViewModel _selectedMessageElement;
            public MessageElementViewModel SelectedMessageElement
            {
                set
                {
                    if (_selectedMessageElement == value) return;
                    this._selectedMessageElement = value;
                    SearchGrid(value.Id, messageFields);

                }
                get
                {
                    return this._selectedMessageElement;
                }

            }
    // search matching Id in the grid

             public void SearchGrid(int id, ObservableCollection<MessageFieldViewModel> msgField)
            {
                 if (msgField.Any())
                    DisplayColor = msgField.Last().Id == id ? Brushes.CadetBlue : Brushes.Black;
            }

XAML: Call the display color property to highlight a matching id.
DataGrid


 <DataGrid   ItemsSource="{Binding MessageFields}" Margin="4,0,380,6" Grid.Row="2" AutoGenerateColumns="False"  IsReadOnly="True"   SelectedValue="{Binding SelectedMessageField, Mode=TwoWay}"
                    RowBackground="{Binding Path=DisplayColor}">

TreeView:



 <TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding MessageElements[0].Children[0].Children,  Mode=TwoWay}"   
                  SelectedItemChanged="TreeView_OnSelectedItemChanged"
                 SelectedValuePath="Id">

Code Behind for treeview select item;



readonly MainWindowModel _mainWindowModel = new MainWindowModel();

    private void TreeView_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (_mainWindowModel != null)
            _mainWindowModel.SelectedMessageElement = (MessageElementViewModel)e.NewValue;
    }

编辑:

     <DataGrid Name="Datagrid"    ItemsSource="{Binding MessageFields}" Margin="4,0,380,6" Grid.Row="2" AutoGenerateColumns="False"  IsReadOnly="True"   SelectedValue="{Binding SelectedMessageField}"
                            AlternatingRowBackground ="{Binding Path=DisplayColor}" RowBackground="{Binding Path=DisplayColor}" Background="CadetBlue">
                    <DataGrid.Columns >
                        <DataGridTextColumn Header="ID" Binding="{Binding Id}" Width="*"  />      <!--Foreground="{Binding Path=DisplayColor}-->

                        <DataGridTextColumn Header="Code" Binding="{Binding Code}" Width="*" />
                        <DataGridTextColumn Header="Field Name" Binding="{Binding Name}" Width="*" />
                        <DataGridTextColumn Header="Position" Binding="{Binding Position}" Width="*"   />
                        <DataGridTextColumn Header="Length" Binding="{Binding Length}" Width="*"  />
         </DataGrid.Columns>
    </DataGrid> 

为什么我的显示颜色属性不适用于匹配的 ID?

4

2 回答 2

1

你可以简单地使用触发器

XAML

    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="Background" Value="Red"/><!--default color-->
            <Style.Triggers>
                <DataTrigger Binding="{Binding DisplayColor}" Value="Black">
                    <Setter Property="Background" Value="Black"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding DisplayColor}" Value="CadetBlue">
                    <Setter Property="Background" Value="CadetBlue"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>

建议您Datagrid.ItemsSourceList<MyClass>MyClass必须包含以下内容Property

public string DisplayColor //don't use Brush
{
    set
    {
        _displayColor = value;
        NotifyPropertyChanged("DisplayColor");
    }

    get { return _displayColor; }

}

进一步的解释在这里

于 2013-06-17T09:00:08.157 回答
0

我想我找到了解决这个问题的方法,但我讨厌它,因为我是从后面的代码中做的。如果我能在我的虚拟机中做到这一点会很好。

    private void TreeView_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {

        foreach (MessageFieldViewModel item in Datagrid.ItemsSource)
        {
            if (_mainWindowModel != null)
                _mainWindowModel.SelectedMessageElement = (MessageElementViewModel) e.NewValue;


            foreach (MessageFieldViewModel rowItem in Datagrid.ItemsSource)
            {
                var row = Datagrid.ItemContainerGenerator.ContainerFromItem(rowItem) as DataGridRow;
                if (_mainWindowModel.SelectedMessageElement != null)
                    if (rowItem.Id == _mainWindowModel.SelectedMessageElement.Id)

                        row.Background = _mainWindowModel.DisplayColor;

                     else
                    row.Background = Brushes.White;

            }
        }


    }
于 2013-06-14T19:39:10.353 回答