繁体   English   中英

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

[英]How to change BitmapEffect in custom WPF control via C# code

我有一个自定义控件类型,例如: <Grid> ... </Grid>和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
}
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;
    }
}

在您的示例中,您编写: //there i want to change Control1.BitmapEffect 请注意,您需要更改Grid的BitmapEffect,而不是Control1的BitmapEffect。

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

这里列出了不同的效果: 不同的BitmapEffect

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM