如何通过 C# 代码更改自定义 WPF 控件中的 BitmapEffect

发布于 2024-08-15 11:52:34 字数 1037 浏览 8 评论 0原文

我有一个自定义控件类型,例如: ; ... 和 Grid.BitmapEffect 属性。如何通过 C# 代码(例如在事件上)更改此控件(网格)中的 BitmapEffetc?

代码示例 - 自定义控件的一部分:

[...]
<Grid Background="#FFE5AA">
    <Grid.RowDefinitions>
        <RowDefinition Height="62*"/>            
        <RowDefinition Height="15*"/>
        <RowDefinition Height="23*"/>
    </Grid.RowDefinitions>

    <Grid.BitmapEffect>
        <OuterGlowBitmapEffect GlowColor="#459E5A" GlowSize="13" Noise="0" Opacity="0.9" />
    </Grid.BitmapEffect>

    <Border Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" BorderBrush="#F5B903" BorderThickness="1,1,1,1" >
    </Border>
[...]

然后在 Window.xaml 中:

<controls:MyControl Name="Control1" Cursor="Hand" MouseDown="Control1_MouseDown" />

然后在 C# 中:

private void Control1_MouseDown(object sender, MouseButtonEventArgs e)
{
    //there i want to change Control1.BitmapEffect
}

I have a custom control type like: <Grid> ... </Grid> and Grid.BitmapEffect property. How can I change BitmapEffetc in this Control (Grid) via C# code (e.g. on event)?

Code sample - part of custom control:

[...]
<Grid Background="#FFE5AA">
    <Grid.RowDefinitions>
        <RowDefinition Height="62*"/>            
        <RowDefinition Height="15*"/>
        <RowDefinition Height="23*"/>
    </Grid.RowDefinitions>

    <Grid.BitmapEffect>
        <OuterGlowBitmapEffect GlowColor="#459E5A" GlowSize="13" Noise="0" Opacity="0.9" />
    </Grid.BitmapEffect>

    <Border Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" BorderBrush="#F5B903" BorderThickness="1,1,1,1" >
    </Border>
[...]

Then in Window.xaml:

<controls:MyControl Name="Control1" Cursor="Hand" MouseDown="Control1_MouseDown" />

Then in C#:

private void Control1_MouseDown(object sender, MouseButtonEventArgs e)
{
    //there i want to change Control1.BitmapEffect
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最冷一天 2024-08-22 11:52:34
myGrid.BitmapEffect = null;

PS:请注意, BitmapEffect 被认为已过时并且应该使用 Effect


这是一个基于您的示例的示例,该示例运行得很好(在我的机器上):只要我在网格内单击,效果就会消失。

XAML:

<Window x:Class="WpfCsApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<Grid Background="#FFE5AA" Margin="10" MouseDown="Grid_MouseDown" x:Name="myGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="62*"/>
        <RowDefinition Height="15*"/>
        <RowDefinition Height="23*"/>
    </Grid.RowDefinitions>
    <Grid.BitmapEffect>
        <OuterGlowBitmapEffect GlowColor="#459E5A" GlowSize="13" Noise="0" Opacity="0.9" />
    </Grid.BitmapEffect>
    <Border Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" BorderBrush="#F5B903" BorderThickness="1,1,1,1" >
        <TextBlock>Test</TextBlock>
    </Border>
</Grid>
</Window>

代码隐藏:

public partial class Window1 : Window {
    public Window1() {
        InitializeComponent();
    }

    private void Grid_MouseDown(object sender, MouseButtonEventArgs e) {
        myGrid.BitmapEffect = null;
    }
}

在您的示例中,您编写://我想更改 Control1.BitmapEffect。请注意,您需要更改 Grid 的 BitmapEffect,而不是 Control1 的 BitmapEffect。

myGrid.BitmapEffect = null;

PS: Note that BitmapEffect is considered obsolete and that Effect should be used instead.


Here's an example based on your sample which works perfectly fine (here on my machine): As soon as I click within the Grid, the effect disappears.

XAML:

<Window x:Class="WpfCsApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<Grid Background="#FFE5AA" Margin="10" MouseDown="Grid_MouseDown" x:Name="myGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="62*"/>
        <RowDefinition Height="15*"/>
        <RowDefinition Height="23*"/>
    </Grid.RowDefinitions>
    <Grid.BitmapEffect>
        <OuterGlowBitmapEffect GlowColor="#459E5A" GlowSize="13" Noise="0" Opacity="0.9" />
    </Grid.BitmapEffect>
    <Border Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" BorderBrush="#F5B903" BorderThickness="1,1,1,1" >
        <TextBlock>Test</TextBlock>
    </Border>
</Grid>
</Window>

Codebehind:

public partial class Window1 : Window {
    public Window1() {
        InitializeComponent();
    }

    private void Grid_MouseDown(object sender, MouseButtonEventArgs e) {
        myGrid.BitmapEffect = null;
    }
}

In your example you write: //there i want to change Control1.BitmapEffect. Note that you need to change the BitmapEffect of the Grid, not the BitmapEffect of Control1.

草莓味的萝莉 2024-08-22 11:52:34

好的,我知道了!我添加了一个 DepencyProperty 'GlowSize' 并通过它简单地更改发光的大小。 :) 工作完美。

OK, I've got it! I was add an DepencyProperty 'GlowSize' and simply change size of glow via it. :) Works perfect.

思念绕指尖 2024-08-22 11:52:34

这里列出了不同的效果:不同的位图效果

Different effects are listed here : Different BitmapEffect

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文