我正在研究WPF应用程序MVVM模式,我必须在TreeView
和Grid
之间创建某种关系。我们的想法是突出显示id等于treenode 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?
答案 0 :(得分: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.ItemsSource
是List<MyClass>
,MyClass
必须包含以下Property
public string DisplayColor //don't use Brush
{
set
{
_displayColor = value;
NotifyPropertyChanged("DisplayColor");
}
get { return _displayColor; }
}
进一步解释HERE
答案 1 :(得分: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;
}
}
}