내 질문에 간단합니다 :
나는 listBox에 일부 DataTemplate
이 포함되어 있습니다.C# WPF : Fire SelectionChanged 이벤트 먼저 다른 이벤트 전에
SelectionChanged = "stationListBox_SelectionChanged"
그리드 내부의 mouseDown 이벤트 (그리드 데이터 템플릿의 일부입니다) :
MouseDown = "Grid_MouseDown"
나는 SelectionChanged
이벤트를 원하는 보시다시피 나는 목록 상자 내부의 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);
}
}
을 그 인 selectionchanged 이벤트 처리기에서 뭔가? – EvAlex
왜냐하면 내가 한 항목을 클릭하거나 두 번 클릭하고 내가 mouseEventArgs 개체 (e.Clicks 속성)이 필요합니다 관찰해야합니다. 그 선택이 먼저 변경되고 mouseDown 이벤트가 발생한 이후보다 먼저 필요합니다. –