删除UserControl中某些控件的模糊效果

时间:2017-10-20 01:25:52

标签: c# wpf xaml

我有以下代码,我从Code Behind创建UI控件。请参阅IsEnabled方法。我只希望用户控件的背景模糊,但问题是它上面的文本块也变得模糊。这是整个代码。请帮忙。

public class UserControlLiveAnalysisBehaviour : System.Windows.Interactivity.Behavior<UserControl>
    {
        private UserControl CustomControlContent;

        private bool IsExecuted;
        protected override void OnAttached()
        {
            AssociatedObject.Loaded += UserControlLoadedHandler;
        }


        private void UserControlLoadedHandler(object sender, RoutedEventArgs e)
        {
            RegisterUserControlActivatedHandler();
        }

        private void RegisterUserControlActivatedHandler()
        {
            CustomControlContent = AssociatedObject;
            if (CustomControlContent == null)
            {
                return;
            }

            var RunDataInstance = RunDataViewModel.Instance;
            RunDataViewModel.RunDataFileSession<string> session = RunDataInstance.GetLiveSessionObject();
            if (session != null)
            {
                if (session.IsLiveAnalysisSession)
                {
                    IsEnabled();
                }
            }
        }

        private void IsEnabled()
        {
            if (!IsExecuted)
            {
                StackPanel stackPanel = new StackPanel();
                ContentControl cc = new ContentControl();
                StackPanel Sp = new StackPanel();
                Sp.HorizontalAlignment = HorizontalAlignment.Center;
                Sp.VerticalAlignment = VerticalAlignment.Center;
                TextBlock tbl = new TextBlock();
                tbl.Text = "Live Analysis is running. Please wait till it completes....";
                tbl.HorizontalAlignment = HorizontalAlignment.Center;
                tbl.VerticalAlignment = VerticalAlignment.Top;
                Panel.SetZIndex(tbl, 1000);
                //tbl.Style = TextBlockStyle();
                Sp.Children.Add(tbl);
                Sp.Background = System.Windows.Media.Brushes.Transparent;
                BlurEffect effect = new BlurEffect();
                effect.Radius = 3;
                CustomControlContent.Effect = effect;
                Sp.Effect = null;
                cc.Content = CustomControlContent.Content;
                stackPanel.Children.Add(cc);
                stackPanel.Children.Add(Sp);
                CustomControlContent.Content = stackPanel;
                IsExecuted = true;
            }
        }
     }

1 个答案:

答案 0 :(得分:2)

您将effect应用于CustomControlContent并将其他内容放入其中,因此效果会影响其中的所有内容。如果您希望某些元素不受此影响,则不能将它们添加为已应用效果的控件的子元素。

不是在后面的代码中创建所有内容,而是为此目的更简单的样式可能更容易维护和更改:

<ContentControl Name="CustomControlContent" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <ContentControl.Style>
        <Style TargetType="ContentControl">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ContentControl">
                                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                    <ContentPresenter 
                                    Content="{TemplateBinding Content}"
                                    ContentTemplate="{TemplateBinding ContentTemplate}"
                                    Cursor="{TemplateBinding Cursor}"
                                    Margin="{TemplateBinding Padding}"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                    Opacity="0.6">
                                        <ContentPresenter.Effect>
                                            <BlurEffect Radius="5"/>
                                        </ContentPresenter.Effect>
                                    </ContentPresenter>
                                    <TextBlock Text="Update, please wait ..." HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
    <ContentControl.Content>
        <Image Source="C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg"/>
    </ContentControl.Content>
</ContentControl>

结果:
注意:我也减少了不透明度,这就是图像变亮的原因

Jellyfish