0

我正在使用装饰层在我的网格(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>

这是装饰用户控件:

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

我真的希望你们能帮助我。

4

1 回答 1

0

IsEnabled 属性仅启用/禁用与元素的交互,与 Visibility 无关。

您应该做的是在您想要隐藏它时将 GridWithRulerxaml 的 Visibility 属性设置为 Collapsed或 Hidden ,并在您希望它显示时将其设置为 Visible。

编辑:对其进行测试,将 AdornerDecorator 内的 GridWithRulerxaml 用户控件的可见性设置为隐藏不会隐藏装饰器。

再想一想,如果这不是您想要的,则可以使用 IsEnabled 属性执行此操作,观察它的变化并根据值添加/删除装饰器。

于 2013-12-12T21:43:11.340 回答