单击按钮时如何将selectedIndex设置为-1?

时间:2016-02-12 15:43:06

标签: wpf datatrigger

当我点击同一视图的按钮时,我希望看到SelectedIndex的{​​{1}}属性。我正在尝试这段代码:

dataGrid

但我单击<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属性?

感谢。

2 个答案:

答案 0 :(得分:1)

IsPressed property only have a get not a set and sometime it can cause problem in triggers.(like other properties of this type)however here it is working fine.

But if you want a different approach try using below:

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

答案 1 :(得分:1)

这是代码线束代码我用来看你的代码工作正常。我在您的xaml周围添加了注释,以便您可以将其与您自己的代码进行匹配。代码只是一个窗口,其中包含基本DataGrid,Button和TextBlock绑定到DataGrid的SelectedIndex。您可以看到,当您按下按钮时,TextBlock的文本将设置为-1,这是您在DataTrigger中设置为SelectedIndex的值。

的Xaml:

-include("ejabberd.hrl").
-include("logger.hrl").

代码背后:

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

在我的评论中,我说Button从ButtonBase继承了IsPressed。我的意思是您可以通过按钮实例访问IsPressed属性,因为Button是ButtonBase的子类,它实现IsPressed