【问题标题】:WPF Custom ControlsWPF 自定义控件
【发布时间】:2013-12-11 14:02:59
【问题描述】:

我一直在尝试解决这个问题。

故事:我有一个MainWindow 和两个用户控件。 当MainWindow 加载时,一个控件可见,而另一个不可见。 一旦用户输入了他们的数据和设置,我需要让另一个表单可见。

需要初始化在启动时不可见的表单,因为它正在从运行它的计算机的 WMI 中收集数据。它还在为用户收集广告信息。

由于某种原因,我无法让一种形式显示另一种形式。

我认为这是我应该看的:

#region Class Variable
    public string ShowSideBar { get { return (String)GetValue(VisibilityProperty); } set { SetValue(VisibilityProperty, value); }}
    public DependencyProperty VisibilityProperty = DependencyProperty.Register("ShowSideBar", typeof(string), typeof(UserControl), null);
    #endregion

这是在我的MainWindow 类中设置的,但是,我不知道为什么我不能从任何其他用户控件调用它。

有什么方法可以从我的 MainWindow 向我的所有表单公开这样的内容吗?

public int RowSpan {
   get { return Grid.GetRowSpan(DockPanel1); }
   set {  Grid.SetRowSpan(DockPanel1,value); }
}

【问题讨论】:

    标签: c# wpf properties


    【解决方案1】:

    依赖属性必须是静态的。为什么类型是string?如果您希望将控件的可见性绑定到它,它不应该是Visibility 吗?

    它必须是依赖属性吗?您也可以使用常规属性并实现 INotifyPropertyChanged,因为您没有将 this 字段绑定到任何东西,而是将其他东西绑定到 it

    对于依赖属性,请尝试以下操作:

    public static readonly DependencyProperty SideBarVisibilityProperty = DependencyProperty.Register("SideBarVisibility", typeof(Visibility), typeof(MyTemplatedControl), null);
    public Visibility SideBarVisibility
    {
        get { return (Visibility)GetValue(SideBarVisibilityProperty); }
        set { SetValue(SideBarVisibilityProperty, value); }
    }
    

    【讨论】:

    • @user3091374,请注意...依赖属性的 TYPEOF 是基于您的 CUSTOM 类,而不仅仅是您派生类的通用“UserControl”。
    【解决方案2】:

    首先,此应用程序将受益于 MVVM 模式的应用程序。

    但是,不采用这种方法,您仍然可以解决您遇到的问题。用户控件依赖于知道其父控件是不寻常的。主窗口后面的代码将是放置此代码的更好位置。 (不如视图模型……但那是另一回事了。)

    将事件ShowSideBar 添加到应使侧边栏可见的控件。在主窗口中附加一个处理程序,并使用该处理程序显示第二个控件。这里根本不需要依赖属性。

    public class MyControl : UserControl
    {
        ...
        public event EventHandler ShowSideBar;
    
        // Call this method when you need to show the side bar.
        public void OnShowSideBar()
        {
            var s = this.ShowSideBar;
            if (s != null)
            {
                s(this, EventArgs.Empty);
            }
        }
    }
    
    public class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            this.FirstControl.ShowSideBar += (s, e) =>
            {
                this.SecondControl.Visibility = Visibility.Visible;
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      我修复了初始化的组件,但正在改变。

      X:Class="AdminTools.MainWindow.ShowSideBar" 到 x:Class="AdminTools.MainWindow"。

      现在我有一个问题

      <Window
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:User="clr-namespace:AdminTools.Controls.User"
              xmlns:Custom="clr-namespace:AdminTools.Controls.Custom"
              xmlns:Bindings="clr-namespace:AdminTools.Functions"
              x:Class="AdminTools.MainWindow"
              Title="MainWindow" Height="691.899" Width="1500"
      
          >
          <Window.DataContext>
              <Bindings:ShowSideBar />
          </Window.DataContext>
      
      <Bindings:ShowSideBar /> = ShowSideBar does not exist in the namespace clr-namespace:AdminTools.Functions
      

      ShowSideBar:成员名称不能与其封闭类型相同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-08
        • 2015-08-28
        • 1970-01-01
        • 1970-01-01
        • 2015-06-03
        • 1970-01-01
        • 2015-09-07
        相关资源
        最近更新 更多