wpf值得注意的ishittestvisible

wpf值得注意的ishittestvisible

這個屬性設置為false,看起來沒有變化,但操作上已經把他完全忽視了,不觸發事件,可以直接點到它下面的東西。

最后更新 2021/10/20 下午6:18
普通的地球人
预计阅读 4 分钟
分类
WPF
标签
.NET WPF HitTest

這個屬性我們平時可能並不怎麼用,先來看下 msdn 上的解釋:

解釋的非常專業,然而我並沒有看懂。

說說我的理解吧:把這個屬性設置為 false,看起來沒有變化,但操作上已經把他完全忽視了,不觸發事件,可以直接點到它下面的東西。

這個屬性能方便的解決工作中常見的麻煩,比如下面這個例子:

)

注意上面那部分.效果很簡單,就是個漸變的效果.但是這個漸變貫穿了兩列,就使得處理起來有點小麻煩.

當然解決方案有很多:

可以寫兩個 listboxitem 的樣式,第一個放頂部有漸變的背景,和右部保持一致,通過樣式選擇器來實現.這顯然比較麻煩.

還可以在大背景下放個漸變,listboxitem 的上半部分做成透明,這樣相對簡單,但不一定能實現理想的效果.

ishittestvisible 屬性就很好的解決了這個問題.直接在上層放個 border,背景設置成漸變,ishittestvisible 設置為 false.這樣就既能看到漸變效果,又能透過 border,直接點到 listboxitem.設置一個屬性就解決了問題,非常方便.相當於在上面放了個蒙板,但是這個蒙板能看到卻點不到.

類似的我還想到了一個場景:

這個效果頂層是個圖片,ishittestvisible 為 false,透明為 0.3.

並不是圖片是個背景,然後所有控制項都是半透明效果.

見代碼:

XMAL:

<Grid>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="70"></RowDefinition>
      <RowDefinition></RowDefinition>
      <RowDefinition Height="50"></RowDefinition>
    </Grid.RowDefinitions>
    <Border Background="#555F5F">
      <label Content="logo" Foreground="White"></label>
    </Border>
    <Grid Grid.Row="1" Background="#AAAFAF">
      <StackPanel
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Button.Click="StackPanel_Click"
      >
        <button Width="132" Height="32" Content="金闪闪" Margin="10"></button>
        <button Width="132" Height="32" Content="小圆" Margin="10"></button>
      </StackPanel>
      <label
        Content="我不透明"
        Background="Green"
        Foreground="Blue"
        Width="100"
        Height="100"
        Margin="76,29,266,171"
      ></label>
      <label
        Content="我不透明"
        Background="Red"
        Foreground="Blue"
        Width="100"
        Height="40"
        Margin="112,40,230,220"
      ></label>
    </Grid>
    <Border Grid.Row="2" Background="#555F5F">
      <label Content="状态栏" Foreground="White"></label>
    </Border>
  </Grid>
  <image
    Name="img"
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    Width="0"
    Height="0"
    Source="/Image/jinshanshan.jpg"
    Stretch="Fill"
    Opacity="0.1"
    IsHitTestVisible="False"
  ></image>
</Grid>

後台:

private void StackPanel_Click(object sender, RoutedEventArgs e)
{
    Button btn = (Button)e.OriginalSource;
    string content = btn.Content.ToString();
    if (content == "金闪闪")
    {
        img.Source = new BitmapImage(new Uri(@"/Image/jinshanshan.jpg", UriKind.Relative));
    }
    if (content == "小圆")
    {
        img.Source = new BitmapImage(new Uri(@"/Image/xiaoyuan.jpg", UriKind.Relative));
    }

    DoubleAnimation daX = new DoubleAnimation();
    daX.From = 0;
    daX.To = 400;
    daX.FillBehavior = FillBehavior.HoldEnd;
    Storyboard.SetTarget(daX, img);
    Storyboard.SetTargetProperty(daX, new PropertyPath(Image.WidthProperty));
    DoubleAnimation daY = new DoubleAnimation();
    daY.From = 0;
    daY.To = 400;
    daY.FillBehavior = FillBehavior.HoldEnd;
    Storyboard.SetTarget(daY, img);
    Storyboard.SetTargetProperty(daY, new PropertyPath(Image.HeightProperty));
    DoubleAnimation daOp = new DoubleAnimation();
    daOp.From = 1;
    daOp.To = 0.3;
    daOp.FillBehavior = FillBehavior.HoldEnd;
    Storyboard.SetTarget(daOp, img);
    Storyboard.SetTargetProperty(daOp, new PropertyPath(Image.OpacityProperty));

    Storyboard sb = new Storyboard();
    sb.Children.Add(daX);
    sb.Children.Add(daY);
    sb.Children.Add(daOp);
    sb.Begin();
}
Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 2025/1/26

wpf 藉助自定義 xml 文件實現國際化

本文詳細居間了在wpf程式中使用自定義xml文件實現國際化的方法,包括安裝必備nuget包、動態獲取語言列表、動態切換語言、在代碼和xaml界面中使用翻譯字符串等內容,同時提供了源碼連結,幫助開發者輕鬆實現wpf應用的國際化。

继续阅读