2014-02-25 59 views
0

我的应用程序中有一个DataGrid和一个ListView。 ListView提供了有关数据网格所选项目的信息。我在该ListView中放置了一个HyperLink,它应该将数据网格的选定项目更改为当前选定项目的“父级”。在xaml中设置datagrid的突出显示的单元格

我的方法是在我的代码后面设置SelectedItem。一切工作得很好,但数据网格不突出显示新的选定项目。但我可以清楚地看到它被选中,因为它的灰色背景色。是否有可能以编程方式设置突出显示的单元格?

<ListView> 
    <TextBlock Text="{Binding SelectedParameter.Definition.Name, StringFormat=Name: {0:C}}" 
       TextWrapping="Wrap"/> 
    <TextBlock Text="{Binding SelectedParameter.Definition.Type, StringFormat=Datentyp: {0:C}}" 
           TextWrapping="Wrap"/> 
       TextWrapping="Wrap"/> 
    <Hyperlink Command="{Binding GoToMasterParameterCommand}"> 
     Masterparameter 
    </Hyperlink> 
</ListView> 

<DataGrid Name="m_DataGrid" 
       ItemsSource="{Binding Path=Parameters}" 
       SelectedItem="{Binding SelectedParameter}" 
       SelectionMode="Single" 
       AutoGenerateColumns="False" 
       TargetUpdated="m_ParameterDataGrid_TargetUpdated"> 
     <DataGrid.Columns> 
      <DataGridTextColumn 
       Header="ID" 
       Binding="{Binding Id}" 
       IsReadOnly="True"/> 
      <DataGridTextColumn 
       Header="Value" 
       Binding="{Binding Value.CurrentInternalValue, NotifyOnTargetUpdated=True}" 
     </DataGrid.Columns> 
    </DataGrid> 

internal void GoToMasterParameter() 
{ 
    string parentId = GetParentId(this.SelectedParameter); 
    this.SelectedParameter = this.Parameters.Single(item => item.Id == parentId); 
} 

回答

0

你的问题是,所选择的行/细胞不被聚焦的焦点仍然在你的ListView项。你可以做的是设计xaml中的DataGridCell元素。下面是证明这一小段代码:

<Window.Resources> 
    <Style TargetType="{x:Type DataGridCell}"> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="True"> 
       <Setter Property="Background" Value="Red"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="Auto"/> 
    </Grid.ColumnDefinitions> 
    <DataGrid ItemsSource="{Binding Tests}" 
       SelectedItem="{Binding GridSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
       SelectedIndex="{Binding SelectedGridIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 

</DataGrid> 
    <Button Command="{Binding ChangeSelectedItemCommand}" 
      Content="Change Grid Selected item" 
      Grid.Column="1" 
      VerticalAlignment="Top"/> 
</Grid> 

,这里的视图模型部分:

public class MainWindowViewModel : INotifyPropertyChanged 
{ 
    #region Private members 

    private List<TestClass> _tests; 
    private TestClass _gridSelectedItem; 
    private ICommand _changeSelectedItemCommand; 
    private int _selectedGridIndex; 
    #endregion 

    #region Constructor 

    public MainWindowViewModel() 
    { 
     Tests = new List<TestClass>(); 
     for (int i = 0; i < 25; i++) 
     { 
      TestClass testClass= new TestClass {Name = "Name " + i, Title = "Title" + i}; 
      Tests.Add(testClass); 
     } 
    } 

    #endregion 

    #region Public properties 
    public List<TestClass> Tests 
    { 
     get { return _tests; } 
     set 
     { 
      _tests = value; 
      OnPropertyChanged("Tests"); 
     } 
    } 

    public TestClass GridSelectedItem 
    { 
     get { return _gridSelectedItem; } 
     set 
     { 
      _gridSelectedItem = value; 
      OnPropertyChanged("GridSelectedItem"); 
     } 
    } 

    public int SelectedGridIndex 
    { 
     get { return _selectedGridIndex; } 
     set 
     { 
      _selectedGridIndex = value; 
      OnPropertyChanged("SelectedGridIndex"); 
     } 
    } 

    #endregion 

    public ICommand ChangeSelectedItemCommand 
    { 
     get { return _changeSelectedItemCommand ?? (_changeSelectedItemCommand = new SimpleCommand(p => ChangeSelectedGridItem())); } 
    } 

    private void ChangeSelectedGridItem() 
    { 
     SelectedGridIndex++; 
    } 

    #region INotifyPropertyChanged 

    public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    #endregion 
} 

演示对象类:

public class TestClass 
{ 
    public string Title { get; set; } 
    public string Name { get; set; } 
} 

和一些命令类:

public class SimpleCommand : ICommand 
{ 

    private readonly Predicate<object> _canExecuteDelegate; 

    private readonly Action<object> _executeDelegate; 

    #region Constructors 

    public SimpleCommand(Action<object> execute) 
     : this(execute, null) 
    { 
    } 

    public SimpleCommand(Action<object> execute, Predicate<object> canExecute) 
    { 
     if (execute == null) 
     { 
      throw new ArgumentNullException("execute"); 
     } 

     _executeDelegate = execute; 
     _canExecuteDelegate = canExecute; 
    } 

    #endregion // Constructors 

    #region ICommand Members 

    public virtual bool CanExecute(object parameter) 
    { 
     return _canExecuteDelegate == null || _canExecuteDelegate(parameter); 
    } 

    public event EventHandler CanExecuteChanged 
    { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 

    public void Execute(object parameter) 
    { 
     _executeDelegate(parameter); 
    } 

    #endregion 
} 

M AKE一定要添加在DataContext视图,使其知道您的视图模型:

public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = new MainWindowViewModel(); 
    } 

我希望这可以帮助你获得预期的效果。

+0

我想我错过了一些东西。这段代码似乎根本没有操纵焦点逻辑。我做了一个相当类似的方法。当我按下编辑SelectedItem属性的链接时,数据绑定会更新数据网格。所以我期望有可能通过数据绑定设置FocusedItem –

+0

这个片段会影响所选项目的背景。您可以只更改背景颜色,并将注意力集中在单击的项目上,或者获取真实的DataGridCell或DataGridRow并将其传递给转换器,您可以在实际的GUI对象上设置.Focus()属性。这会将焦点从listview项目移动到DataGridRow/Cell –

相关问题