简体   繁体   English

WPF中的Adorner,isEnabled无法正常工作吗?

[英]Adorner in WPF, isEnabled not working?

I'm using an adorner layer to make a grid (like, boxes across the entire screen) across my grid (WPF grid). 我正在使用装饰层在我的网格(WPF网格)上创建一个网格(例如整个屏幕上的框)。

I want this to be shown ONLY when a checkbox is marked. 我希望仅在选中复选框后才显示。 However, when I bind the "IsEnabled" property, nothing happens. 但是,当我绑定“ IsEnabled”属性时,没有任何反应。 Even if I set the IsEnabled to false, the grid overlay is still shown! 即使将IsEnabled设置为false,仍然会显示网格覆盖!

This is how I do from my mainWindow.xaml, note the IsEnabled is set to false, but it is still showing up: 这是我在mainWindow.xaml中执行的操作,请注意IsEnabled设置为false,但仍在显示:

<Grid>
        <!--Adornerdecorator is made to make sure the adorner is in the background, and not the foreground-->
        <AdornerDecorator>
          <View:GridWithRulerxaml IsEnabled="False" />
        </AdornerDecorator>
        <ItemsControl ItemsSource="{Binding Classes}"/>
        <ItemsControl ItemsSource="{Binding Edges}"/>
    </Grid>

this is the adorner usercontrol: 这是装饰用户控件:

namespace UMLDesigner.View
{
/// <summary>
/// Interaction logic for GridWithRulerxaml.xaml
/// </summary>
public partial class GridWithRulerxaml : UserControl
{
    public GridWithRulerxaml()
    {
        InitializeComponent();

        //Loaded event is necessary as Adorner is null until control is shown.
        Loaded += GridWithRulerxaml_Loaded;

    }

    void GridWithRulerxaml_Loaded(object sender, RoutedEventArgs e)
    {
        var adornerLayer = AdornerLayer.GetAdornerLayer(this);
        var rulerAdorner = new AlignmentAdorner(this);
        adornerLayer.Add(rulerAdorner);
    }
}
}

And finally the adorner itself: 最后是装饰器本身:

namespace UMLDesigner.Utilities
{
public class AlignmentAdorner : Adorner
{
    private FrameworkElement element;
    public AlignmentAdorner(UIElement el)
        : base(el)
    {
        element = el as FrameworkElement;
    }

    protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);

        double height = element.ActualHeight;
        double width = element.ActualWidth;

        double linesHorizontal = height / 50;
        double linesVertical = width / 50;

        var pen = new Pen(Brushes.LightGray, 1) { StartLineCap = PenLineCap.Triangle,     EndLineCap = PenLineCap.Triangle };

        int offset = 0;

        for (int i = 0; i <= linesVertical; ++i)
        {
            offset = offset + 50;
            drawingContext.DrawLine(pen, new Point(offset, 0), new Point(offset, height));
        }

        offset = 0;

        for (int i = 0; i <= linesHorizontal; ++i)
        {
            offset = offset + 50;
            drawingContext.DrawLine(pen, new Point(0, offset), new Point(width, offset));
        }
    }
}
}

I really hope you can help me out here guys. 我真的希望您能在这里帮助我。

The IsEnabled property only enables/disables interaction with the element, and has nothing to do with the Visibility. IsEnabled属性仅启用/禁用与元素的交互,与可见性无关。

What you should do is set the Visibility property of the GridWithRulerxaml to Collapsed or Hidden when you want to hide it, and set it to Visible when you want it shown. 您应该执行的操作是在要隐藏GridWithRulerxaml时将其“可见性”属性设置为“折叠” 或“隐藏” ,并在显示时将其设置为“可见”。

Edit: Tested it, setting the visiblity to Hidden of the GridWithRulerxaml usercontrol inside the AdornerDecorator doesn't hide the adorner. 编辑:对其进行了测试,将AdornerDecorator内部的GridWithRulerxaml用户控件的可见性设置为Hidden不会隐藏装饰器。

And thinking some more about it, if this isn't what you want, it might be possible to do this with the IsEnabled property, watching for changes in it and adding/removing the adorner depending on the value. 然后再考虑一下,如果这不是您想要的,则可以使用IsEnabled属性来执行此操作,观察其中的更改并根据值添加/删除装饰器。

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

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