0

私は現在、ツリービューとグリッドの間に何らかの関係を作成する必要がある WPF アプリケーション MVVM パターンに取り組んでいます。関係は ID に基づいています。アイデアは、ツリーノード ID と等しい ID を持つ行を強調表示することです。

色のプロパティを表示する

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

        get { return _displayColor; }

    }

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

    }

// グリッドで一致する ID を検索します

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

XAML: display color プロパティを呼び出して、一致する ID を強調表示します。データグリッド

ツリー表示:

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

                  SelectedItemChanged="TreeView_OnSelectedItemChanged">

ツリービュー選択項目のコード ビハインド。

    readonly MainWindowModel _mainWindowModel = new MainWindowModel();

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

編集:

<DataGrid   ItemsSource="{Binding MessageFields}" Margin="4,0,380,6" Grid.Row="2" AutoGenerateColumns="False"  IsReadOnly="True"   SelectedValue="{Binding SelectedMessageField}"
                    RowBackground="{Binding Path=DisplayColor}">
            <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>

一致する ID に対して表示色プロパティが機能しないのはなぜですか?

皆さん、ありがとうございました。

4

1 に答える 1

2

MVVM パターンを使用している場合は、ViewModel で Windows、DataGrids、 Brushesなどの VisualTree からのものを定義しないでください。

それでは、あなたの質問に

これは簡単な例です:
データグリッドの RowBackground 色を変更する方法

XAML

    <DataGrid ItemsSource="{Binding Source}">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="Background" Value="Red"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ColorSwitch}" Value="false">
                        <Setter Property="Background" Value="Green"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>
    </DataGrid>

コードビハインド

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new VM();
    }
}

ビューモデル

public class VM
{
    public List<myItem> Source {get;set;}

    public VM()
    {
        Source = new List<myItem>();
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=false});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=false});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=true});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=false});
        Source.Add(new myItem{Field1 = "some Text", Field2 = "some other Text",ColorSwitch=true});
    }
}

シンプルオブジェクト

public class myItem
{
    public string Field1 {get;set;}
    public string Field2 {get;set;}
    public bool ColorSwitch {get;set;}
}
于 2013-06-17T08:43:27.113 に答える