1

我的问题很简单:
我有一个 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);
    }
}
4

1 回答 1

2

您可以使用 Dispatcher 对象:

    private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
    {
        this.Dispatcher.BeginInvoke(new Action(delegate
        {
            // do stuff
        }
        ));
    }
于 2012-04-11T16:37:13.340 回答