I would like to see the SelectedIndex
property of the dataGrid
when I click a button of the same view. I am trying this code:
<DataGrid.Style>
<Style TargetType="DataGrid">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=myButton, Path=IsPressed}" Value="True">
<Setter Property="SelectedIndex" Value="-1"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Style>
Butn I click the button the selectedIndex
is not update. I think that the dataTrigger
, how is set in the style, it is not fired, but really I am not sure.
How can I set the selectedIndex
property when I click my button?
Thanks.
Here is the code harness code I used to see that your code works fine. I put comments around your xaml so you can match it up with your own code. The code is just a window with a basic DataGrid, Button and TextBlock bound to the SelectedIndex of your DataGrid. You can see that when you press the button the text of the TextBlock will be set -1, which is what you are setting as the value of your SelectedIndex in your DataTrigger.
Xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns:local="clr-namespace:WpfApplication1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:IntToStringConverter x:Key="intoStringConverter"/>
</Window.Resources>
<StackPanel>
<DataGrid Name="dgUsers">
<!-- Copy and paste of your code -->
<DataGrid.Style>
<Style TargetType="DataGrid">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=myButton, Path=IsPressed}" Value="True">
<Setter Property="SelectedIndex" Value="-1"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Style>
<!---End of copy paste-->
</DataGrid>
<Button Name="myButton">Reset Index</Button>
<StackPanel Orientation="Horizontal">
<TextBlock Text="SelectedIndex: "/>
<TextBlock Text="{Binding ElementName=dgUsers, Path=SelectedIndex, Converter={StaticResource intoStringConverter}}"/>
</StackPanel>
</StackPanel>
</Window>
Code behind:
using System;
using System.Windows;
using System.Windows.Data;
using System.Globalization;
using System.Collections.Generic;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Test> testList = new List<Test>();
testList.Add(new Test() { ID = 1, Name = "first" });
testList.Add(new Test() { ID = 2, Name = "second" });
testList.Add(new Test() { ID = 3, Name = "third" });
dgUsers.ItemsSource = testList;
}
}
public class Test
{
public int ID { get; set; }
public string Name { get; set; }
}
[ValueConversion(typeof(int), typeof(string))]
public class IntToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return "NULL";
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
In my comment where I said that Button inherits IsPressed from ButtonBase. I meant that you can access the IsPressed property through an instance of a button since a Button is a subclass of ButtonBase which implements IsPressed.