单击按钮时如何将 selectedIndex 设置为 -1?
How to set the selectedIndex to -1 when I click a button?
我想在单击同一视图的按钮时看到 dataGrid
的 SelectedIndex
属性。我正在尝试这段代码:
<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>
但是我点击按钮 selectedIndex
没有更新。我认为 dataTrigger
,如何在样式中设置,它不会被触发,但我真的不确定。
单击按钮时如何设置 selectedIndex
属性?
谢谢。
IsPressed
属性 只有 get
而不是 set
有时它会导致触发器出现问题。( 就像这个的其他属性type) 但是在这里它工作正常。
But 如果您想要不同的方法,请尝试使用以下方法:
<DataGrid ItemsSource="{Binding BadFoldersHistory}" Name="list" Height="200" SelectedIndex="{Binding ElementName=myButton,Path=Tag}" >
</DataGrid>
<Button Content="click" Height="100" Width="200" Name="myButton">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<Int32Animation Storyboard.TargetProperty="Tag" To="-1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
这是我用来查看您的代码工作正常的代码工具代码。我在您的 xaml 周围添加了注释,以便您可以将其与您自己的代码相匹配。该代码只是一个 window,带有绑定到 DataGrid 的 SelectedIndex 的基本 DataGrid、Button 和 TextBlock。您可以看到,当您按下按钮时,TextBlock 的文本将设置为 -1,这就是您在 DataTrigger 中设置为 SelectedIndex 的值。
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>
后面的代码:
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();
}
}
}
在我的评论中,我说 Button 从 ButtonBase 继承了 IsPressed。我的意思是您可以通过按钮的实例访问 IsPressed 属性,因为 Button 是实现 IsPressed.
的 ButtonBase 的子类
我想在单击同一视图的按钮时看到 dataGrid
的 SelectedIndex
属性。我正在尝试这段代码:
<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>
但是我点击按钮 selectedIndex
没有更新。我认为 dataTrigger
,如何在样式中设置,它不会被触发,但我真的不确定。
单击按钮时如何设置 selectedIndex
属性?
谢谢。
IsPressed
属性 只有 get
而不是 set
有时它会导致触发器出现问题。( 就像这个的其他属性type) 但是在这里它工作正常。
But 如果您想要不同的方法,请尝试使用以下方法:
<DataGrid ItemsSource="{Binding BadFoldersHistory}" Name="list" Height="200" SelectedIndex="{Binding ElementName=myButton,Path=Tag}" >
</DataGrid>
<Button Content="click" Height="100" Width="200" Name="myButton">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<Int32Animation Storyboard.TargetProperty="Tag" To="-1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
这是我用来查看您的代码工作正常的代码工具代码。我在您的 xaml 周围添加了注释,以便您可以将其与您自己的代码相匹配。该代码只是一个 window,带有绑定到 DataGrid 的 SelectedIndex 的基本 DataGrid、Button 和 TextBlock。您可以看到,当您按下按钮时,TextBlock 的文本将设置为 -1,这就是您在 DataTrigger 中设置为 SelectedIndex 的值。
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>
后面的代码:
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();
}
}
}
在我的评论中,我说 Button 从 ButtonBase 继承了 IsPressed。我的意思是您可以通过按钮的实例访问 IsPressed 属性,因为 Button 是实现 IsPressed.
的 ButtonBase 的子类