我想创建一个可以通过xaml接收参数的用户控件。
我搜索并尝试了一些事情,但它们似乎都不适合我。 我附上了重要的代码。 我的用户控制代码(只有依赖属性):
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");
}
}
来自主窗口的电话:
<StackPanel>
<local:TablesForm DatabaseName="Testing"/>
</StackPanel>
当我将用户控件construcor中的字符串直接放入dependencyProperty中时,它可以工作。 如果能有人解释背后的逻辑(并帮助解决我的问题),那将是很棒的。
感谢您的帮助。
答案 0 :(得分:1)
UserControl的工作实现是这个
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();
}
}
}
答案 1 :(得分:0)
我终于得到了它的工作,所有我错过的是添加impelementation到Propertycallbackchanged。我没有必要设置元素名称。我的意思是,我所要做的就是改变:
public static readonly DependencyProperty DatabaseNameProperty = DependencyProperty.Register(
"DatabaseName", typeof(string), typeof(TablesForm), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsArrange));
对此:
public static readonly DependencyProperty DatabaseNameProperty = DependencyProperty.Register(
"DatabaseName", typeof(string), typeof(TablesForm), new FrameworkPropertyMetadata(String.Empty, OnCurrentTimePropertyChanged));
并实现OnCurrentTimePropertyChanged
private static void OnCurrentTimePropertyChanged(DependencyObject source,
DependencyPropertyChangedEventArgs e)
{
TablesForm control = source as TablesForm;
control.m_viewModel.Log = (string)e.NewValue;
}
这将帮助我操作DependencyProperty。
感谢@Benj的帮助。