2013-05-22 49 views
2

已经有一段时间,我正在尝试做类似的事情,现在我正在抛出一个NotImplementedException ...我的意思是,扔在毛巾上。你是我最后的希望。在已禁用的WPF中处理单击事件或MouseDown StackPanel

所以,我的目标是:当用户点击它时启用StackPanel以实现用户友好的目的。

我在这个禁用的StackPanel中有一些项目,它位于窗口内的Grid内的ScrollViewer内部。

有我的代码的理解部分:

<Grid x:Name="MainGrid"> 
    <Grid.RowDefinitions> 
     <RowDefinition MinHeight="30" MaxHeight="30"/> 
     <RowDefinition MinHeight="22" MaxHeight="22"/> 
     <RowDefinition MinHeight="155" Height="155"/> 
     <RowDefinition MinHeight="130"/> 
     <RowDefinition MinHeight="25" MaxHeight="25"/> 
     <RowDefinition MinHeight="22" MaxHeight="22"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition MinWidth="355" Width="360" /> 
     <ColumnDefinition MinWidth="330" /> 
     <ColumnDefinition MinWidth="280" /> 
    </Grid.ColumnDefinitions> 
    <ScrollViewer Grid.Column="2" Grid.Row="3" VerticalScrollBarVisibility="Auto"> 
     <StackPanel IsEnabled="False" IsEnabledChanged="ioStackPanel_IsEnabledChanged"> 
      <Grid Height="22"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="110"/> 
        <ColumnDefinition/> 
        <ColumnDefinition Width="50"/> 
        <ColumnDefinition Width="50"/> 
        <ColumnDefinition Width="20"/> 
       </Grid.ColumnDefinitions> 
      </Grid> 
     </StackPanel> 
    </ScrollViewer> 
</Grid> 

我想这样的事情已经:

<ScrollViewer Grid.Column="2" Grid.Row="3" VerticalScrollBarVisibility="Auto" MouseDown="ScrollViewer_MouseDown"> 

private void ScrollViewer_MouseDown(object sender, MouseButtonEventArgs e) 
{ 
    MessageBox.Show("Well done bro!"); 
} 

但没有什么提高。没有消息框显示。

+0

当您设置的IsEnabled为false,控制将停止响应输入。所以第一个不要禁用堆栈面板。 – cvraman

回答

2

下面的代码工作正常,请按照它。

<Grid x:Name="MainGrid"> 
    <Grid.RowDefinitions> 
     <RowDefinition MinHeight="30" MaxHeight="30" /> 
     <RowDefinition MinHeight="22" MaxHeight="22" /> 
     <RowDefinition Height="155" MinHeight="155" /> 
     <RowDefinition MinHeight="130" /> 
     <RowDefinition MinHeight="25" MaxHeight="25" /> 
     <RowDefinition MinHeight="22" MaxHeight="22" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="360" MinWidth="355" /> 
     <ColumnDefinition MinWidth="330" /> 
     <ColumnDefinition MinWidth="280" /> 
    </Grid.ColumnDefinitions> 
    <ScrollViewer Grid.Row="3" 
        Grid.Column="2" 
        PreviewMouseLeftButtonDown="InnerPanel_PreviewMouseLeftButtonDown_1" 
        VerticalScrollBarVisibility="Auto"> 
     <StackPanel Name="InnerPanel" 
        Background="Gray" 
        IsEnabled="False" 
        IsEnabledChanged="StackPanel_IsEnabledChanged_1"> 
      <Grid> 

       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="110" /> 
        <ColumnDefinition /> 
        <ColumnDefinition Width="50" /> 
        <ColumnDefinition Width="50" /> 
        <ColumnDefinition Width="20" /> 
       </Grid.ColumnDefinitions> 
       <Button Grid.Column="2" Content="Inner Panel" /> 
      </Grid> 
     </StackPanel> 
    </ScrollViewer> 
</Grid> 


private void InnerPanel_PreviewMouseLeftButtonDown_1(object sender, MouseButtonEventArgs e) 
    { 

    } 

这工作正常,并在上述事件中实现您的逻辑。

+0

谢谢拉梅什!我尝试了一切,但预览事件! – Marc

+0

因此,这很烦人,因为每次用户点击时都会引发该事件。但有一个简单的条件“if(item is not enabled)then enable it”it works fine! – Marc

0

不幸的是,当一个控件被禁用时,它会消耗所有的事件并且什么也不做。

如何使用网格将其放置在透明面板后面。当用户点击面板时,你隐藏面板并启用你的堆叠面板。

喜欢的东西...

<scrollviewer ...> 
    <grid> <!--New grid just to align the two panels--> 
    <stackpanel x:Name="enableMe" IsEnabled="False" ...> 
     .... 
    </stackpanel> 
    <border x:name="hideMe" Background="Transparent" Click="EnableStackPanel" ZIndex="1"/> 
</grid> 
</stackpanel> 

public void EnableStackPanel(...) 
{ 
    enableMe.IsEnabled=true; 
    hideMe.Visibilty = Visibility.Collapsed; 
} 
+0

我不想尝试这样的事情,但我几乎在Ramesh的回答之前做过。 – Marc