How to Bind to a Custom Controls Button Visibility from Within Another Control

前端 未结 3 1251
后悔当初
后悔当初 2021-01-19 03:38

I have a custom control, which has a button:



        
相关标签:
3条回答
  • 2021-01-19 03:59

    If you do not want to define a property in the UserControl, which you can always create attached dependency property, and you can declare it in a separate class under the common namespace.

    Something like this:

    MainWindow.xaml

    <local:TestUserControl AttachedProperties:ButtonExt.Visibility="Visible" />
    

    TestUserControl.xaml

    <Button Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, 
                                 Path=(AttachedProperties:ButtonExt.Visibility)}"
            Content="TestButton" />
    

    Attached property definition:

    public static class ButtonExt
    {
        public static readonly DependencyProperty VisibilityProperty;
    
        public static void SetVisibility(DependencyObject DepObject, Visibility value)
        {
            DepObject.SetValue(VisibilityProperty, value);
        }
    
        public static Visibility GetVisibility(DependencyObject DepObject)
        {
            return (Visibility)DepObject.GetValue(VisibilityProperty);
        }
    
        static ButtonExt()
        {
            PropertyMetadata VisibiltyPropertyMetadata = new PropertyMetadata(Visibility.Collapsed);
    
            VisibilityProperty = DependencyProperty.RegisterAttached("Visibility",
                                                                typeof(Visibility),
                                                                typeof(ButtonExt),
                                                                VisibiltyPropertyMetadata);
        }
    }
    

    Some notes about code-behind in MVVM

    I agree with @dkozl, his example does not violate the principle of MVVM, in some cases, the code is present in the View, for example (personally, I always try to avoid code-behind):

    • Installation DataContext.

    • The use of different patterns such as Mediator, Proxy, etc.

    • Determination of properties and behaviors that pertain only to View (as in your case).

    The most important thing when you use the code-behind, it is that all actions possible through ViewModel occurred, ie in ViewModel contains all the logic and for example, in the View click event, call the function, which is in ViewModel.

    For more information about code-behind, please see the answers to the recent question:

    WPF MVVM Code Behind

    0 讨论(0)
  • 2021-01-19 04:02

    You can create DependencyProperty in your UserControl:

    public partial class SomeView : UserControl
    {
        ...
    
        public static DependencyProperty ButtonVisibilityProperty = DependencyProperty.Register("ButtonVisibility", typeof(Visibility), typeof(SomeView));
    
        public Visibility ButtonVisibility
        {
            get { return (Visibility)GetValue(ButtonVisibilityProperty); }
            set { SetValue(ButtonVisibilityProperty, value); }
        }
    }
    

    bind it to Button.Visibility:

    <UserControl x:Class="WpfApplication2.SomeView"
                 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">
        <Button Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=ButtonVisibility}" Content="My Button"/>
    </UserControl>
    

    and then you can control Visibility from outside like so:

    <local:SomeView ButtonVisibility="Collapsed"/>
    

    and because it's a DependencyProperty you can use Binding as well

    0 讨论(0)
  • 2021-01-19 04:10

    Hi just create a bool or Visibility Type property in UserControl1 and set it in Usercontrol2 like

    UserControl1 xaml

    <UserControl x:Class="WpfApplication4.UserControl1"
             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">
    <Grid>
        <Button x:Name="Loadbutton" Content="load"/>
    </Grid>
    

    xaml.cs

     public UserControl1()
        {
            InitializeComponent();
        }
    
        bool showLoadButton;
        public bool ShowLoadButton
        {
            get { return showLoadButton; }
            set
            {
                showLoadButton = value;
                if (showLoadButton)
                    Loadbutton.Visibility = Visibility.Visible;
                else
                    Loadbutton.Visibility = Visibility.Collapsed;
            }
    
        }
    

    UserControl2 Set ShowLoadButton True or false

    <UserControl x:Class="WpfApplication4.UserControl2"
             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" 
             xmlns:local="clr-namespace:WpfApplication4"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <local:UserControl1 ShowLoadButton="True"/>
    </Grid>
    

    0 讨论(0)
提交回复
热议问题