简体   繁体   中英

How to use DependencyProperty in user Control

I want to create a user control that can recieve a parameter via xaml.

i search and tried a few things, bu none of them seems to work for me. i'm attaching the important code. My user control Code (only the dependency property):

 public static readonly DependencyProperty DatabaseNameProperty = DependencyProperty.Register(
        "DatabaseName", typeof(string), typeof(TablesForm), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsArrange));
    public string DatabaseName
    {
        get { return (string)GetValue(DatabaseNameProperty); }
        set
        {
            SetValue(DatabaseNameProperty, value);
            NotifyPropertyChanged("DatabaseName");
        }
    }

The call From Main Window:

<StackPanel>
     <local:TablesForm DatabaseName="Testing"/>
</StackPanel>

When i put in the user control construcor the string directly into the dependencyProperty it works. It would be great if can someone explain the logic behind (and help me with my problem)

Thank for the help.

A working implementation of a UserControl is this

XAML UserControl1

<UserControl x:Class="WpfApplication1.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" 
             Name="_this"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Background="LightGray" Text="{Binding ElementName=_this, Path=DatabaseName}" />
    </Grid>
</UserControl>

CSharp UserControl1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public static readonly DependencyProperty DatabaseNameProperty =
            DependencyProperty.Register
            (
                @"DatabaseName",
                typeof(String),
                typeof(UserControl1),
                new PropertyMetadata(String.Empty)
            );
        public String DatabaseName
        {
            get { return GetValue(DatabaseNameProperty) as String; }
            set { SetValue(DatabaseNameProperty, value); }
        }

        public UserControl1()
        {
            InitializeComponent();
        }
    }
}

XAML TestWindow

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525"
        >
    <Grid>
        <local:UserControl1 DatabaseName="Database_Test" />
    </Grid>
</Window>

CSharp TestWindow

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

在此处输入图片说明

I finally got it to work, all i've missed is to add impelementation to Propertycallbackchanged. i didn't have to set the element name. I mean, all i had to do is to change:

public static readonly DependencyProperty DatabaseNameProperty = DependencyProperty.Register(
    "DatabaseName", typeof(string), typeof(TablesForm), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsArrange));

To this:

 public static readonly DependencyProperty DatabaseNameProperty = DependencyProperty.Register(
        "DatabaseName", typeof(string), typeof(TablesForm), new FrameworkPropertyMetadata(String.Empty, OnCurrentTimePropertyChanged));

and implement OnCurrentTimePropertyChanged

private static void OnCurrentTimePropertyChanged(DependencyObject source,
    DependencyPropertyChangedEventArgs e)
    {
        TablesForm control = source as TablesForm;
        control.m_viewModel.Log = (string)e.NewValue;            
    }

This will help me to manipulate the DependencyProperty.

Thank @Benj for the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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