简体   繁体   English

如何将可见性绑定到在用户控件中作为参数传递的元素?

[英]How to bind Visibility to element which is passed as parameter in User Control?

I have User control which looks like this: 我有如下User control

public partial class TopBarUserControl : UserControl
{
    public System.Windows.Visibility menuVisibility { get; set; }

    public TopBarUserControl()
    {
        InitializeComponent();
    }
}

And I render it in my other page with: 然后在我的其他页面中使用:

<local:TopBarUserControl />

It works fine but I want to hide some part of my Control. 它工作正常,但我想隐藏控件的某些部分。 So I pass the parameter to control with: 所以我通过传递参数来控制:

<local:TopBarUserControl menuVisibility="Collapsed" />

But I don't know how to make it works fine (hide my elements). 但是我不知道如何使其正常工作(隐藏我的元素)。 What I have tried: 我尝试过的

in xaml control: 在xaml控件中:

Visibility="{Binding menuVisibility}"

or set it in code behind but I don't know where it should be setted. 或将其设置在code behind但我不知道应该在哪里设置。

Two things here, first you should ideally declare the property as a DependencyProperty , so you can bind against it and get automatic change notification to update the UI at the right time. 这里有两件事,首先,您应该理想地将该属性声明为DependencyProperty ,以便可以对其进行绑定并获得自动更改通知以在适当的时间更新UI。 Then, you need to correct the binding, so that it points to the property declared at the UserCoontrol level (as you wrote it, it binds to the DataContext , which may not be set. 然后,您需要更正绑定,以便它指向在UserCoontrol级别声明的属性(在编写该属性时,它绑定到可能未设置的DataContext

Try this, in XAML: 在XAML中尝试以下操作:

<UserControl x:Class="WpfApplication1.TopBarUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             x:Name="MainControl">   <!--Give a XAML name to the whole control to bind to properties declared in code-behind-->

    <Menu Visibility="{Binding ElementName=MainControl, Path=MenuVisibility}"/>
</UserControl>

Notice the binding has an ElementName matching the x:Name at the control level. 请注意,绑定具有在控件级别与x:Name匹配的ElementName The convert your property to DependencyProperty : 将您的属性转换为DependencyProperty

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class TopBarUserControl : UserControl
    {
        public static readonly DependencyProperty MenuVisibilityProperty = DependencyProperty.Register("MenuVisibility", typeof(Visibility), typeof(TopBarUserControl), null);

        public Visibility MenuVisibility
        {
            get { return (Visibility)GetValue(MenuVisibilityProperty); }
            set { SetValue(MenuVisibilityProperty, value); }
        }

        public TopBarUserControl()
        {
            InitializeComponent();
        }
    }
}

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

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