C#WPF:Fire SelectionChanged Event First Before Before Before Events

时间:2012-04-11 10:25:19

标签: c# wpf events listbox listboxitem

我的问题很简单:  
我有一个listBox包含一些DataTemplate。  如您所见,我在列表框中有SelectionChanged个事件:

SelectionChanged = "stationListBox_SelectionChanged"

和网格内的mouseDown事件(网格是数据模板的一部分):

MouseDown = "Grid_MouseDown"

我希望SelectionChanged事件首先被解雇,然后是MouseDown事件。    那是因为我想对所选项目做点什么,所以我首先要知道我选择了谁。

XML:

<ListBox x:Name="stationListBox" Grid.Row="0" ItemsSource="{Binding}" SelectionChanged="stationListBox_SelectionChanged" Background="#FF5C7591"                             
    VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
    <ListBox.Resources>
        <DataTemplate DataType="{x:Type viewModels:StationViewModel}">
            <Grid Margin="0,0,0,2"  IsEnabled="{Binding IsEnabled, Mode=TwoWay}" MouseDown="Grid_MouseDown" >
            <Frame BorderBrush="#B935373F" BorderThickness="4"></Frame>
                <StackPanel Orientation="Vertical" Margin="10" >                
                    <StackPanel Orientation="Horizontal">
                        <Image Width="30" Height="30" Margin="3,0" Source="Images\station.png" HorizontalAlignment="Left" />
                        <TextBlock Text="{Binding Name}" FontSize="14" TextAlignment="Left" Width="80" FontFamily="Arial"/>
                            <Grid>
                                <Image Width="20" Height="20" Margin="3,0" Source="Images\turnOn.png" HorizontalAlignment="Right" Visibility="{Binding IsStationOpen, Mode=TwoWay}" />
                                <Image Width="20" Height="20" Margin="3,0" Source="Images\turnOff.png" HorizontalAlignment="Right" Visibility="{Binding IsStationClose, Mode=TwoWay}" />
                               </Grid>
                        <Image Width="20" Height="20" Margin="3,0" Source="Images\delete2.png" HorizontalAlignment="Right" Visibility="{Binding Mode=TwoWay, Path=IsSelected}"
                            ToolTip="Delete station" MouseDown="Image_MouseDown" />
                    </StackPanel>                
                    <StackPanel Name="_editStationGrid" Orientation="Vertical" Visibility="{Binding IsEditable,Mode=TwoWay}">
                        <Expander Visibility="Visible" IsExpanded="{Binding IsExpandered, Mode=TwoWay}">
                            <Expander.Header>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock>Edit Station:</TextBlock>
                                </StackPanel>
                            </Expander.Header>
                            <StackPanel Orientation="Vertical">
                                <StackPanel Orientation="Horizontal" Margin="20,5,5,5" >
                                    <Label Content="Name:"/>
                                    <TextBox MinWidth="120" Text="{Binding Name, Mode=TwoWay}"></TextBox>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="20,0,5,0" >
                                    <Label Content="IP:" Margin="0,0,20,0"/>
                                    <TextBox MinWidth="120" Text="{Binding IP, Mode=TwoWay}" ></TextBox>
                                </StackPanel>
                            </StackPanel>
                        </Expander>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListBox.Resources>
</ListBox>

代码:

private void stationListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    this._selectedStation = ((StationViewModel)this.stationListBox.SelectedItem);
}

private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (inEditMode && e.ClickCount == 1)
    {
        if (_selectedStation.IsSelected == System.Windows.Visibility.Visible)
            _selectedStation.IsSelected = System.Windows.Visibility.Collapsed;
        else
            _selectedStation.IsSelected = System.Windows.Visibility.Visible;
    }

    if (!inEditMode && e.ClickCount == 2)
    {
        if (_selectedStation_Event != null)
            _selectedStation_Event(_selectedStation);
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用Dispatcher对象:

    private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
    {
        this.Dispatcher.BeginInvoke(new Action(delegate
        {
            // do stuff
        }
        ));
    }