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

时间:2013-11-14 19:17:42

标签: c# wpf user-controls adorner

我正在使用一个装饰图层在我的网格(WPF网格)中制作一个网格(比如整个屏幕上的方框)。

我希望仅在标记复选框时显示此项。但是,当我绑定“IsEnabled”属性时,没有任何反应。即使我将IsEnabled设置为false,仍会显示网格覆盖!

这是我在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>

这是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);
    }
}
}

最后是装饰者本身:

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));
        }
    }
}
}

我真的希望你能在这里帮助我。

1 个答案:

答案 0 :(得分:0)

IsEnabled属性仅启用/禁用与元素的交互,并且与可见性无关。

如果要隐藏它,请将GridWithRulerxaml的Visibility属性设置为Collapsed 或Hidden ,并在需要时将其设置为Visible。

编辑:测试它,在AdornerDecorator中将Visiblity设置为隐藏GridWithRulerxaml用户控件不会隐藏装饰器。

考虑更多关于它的信息,如果这不是您想要的,可以使用IsEnabled属性执行此操作,观察其中的更改并根据值添加/删除装饰器。