【发布时间】:2019-10-08 10:46:15
【问题描述】:
所以我得到了这个控制:
CharacterMapControl.xaml:
<UserControl x:Class="CharacterMap.CharacterMapControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CharacterMap">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="350"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBlock Text=""></TextBlock>
</StackPanel>
</Grid>
</UserControl>
然后是 CharacterMapControl.xaml.cs:
using System.Windows;
using System.Windows.Controls;
namespace CharacterMap
{
/// <summary>
/// Interaction logic for CharacterMapControl.xaml
/// </summary>
///
public partial class CharacterMapControl : UserControl
{
public static readonly DependencyProperty FilepathProperty = DependencyProperty.Register("Filepath", typeof(string), typeof(CharacterMapControl));
public string Filepath
{
get { return (string)GetValue(FilepathProperty); }
set { SetValue(FilepathProperty, value); }
}
public CharacterMapControl()
{
InitializeComponent();
}
}
}
这是在 .NET Core 的 WPF 用户控件库中。
然后我添加了一个新的 WPF App .NET Core 项目并编辑了 MainWindow.xaml,如下所示:
<Window x:Class="WPF_Control_Tester.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:charactermap="clr-namespace:CharacterMap;assembly=CharacterMap"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<charactermap:CharacterMapControl Filepath="D:\\repos\\WpfProjects\\latinchars.xml"></charactermap:CharacterMapControl>
</Grid>
</Window>
嗯 - 现在 CharacterMapControl.xaml.cs 中的文件路径始终为空。我不明白为什么。它已正确绑定,应该映射到我在 MainWindow 中添加的文件路径,还是?
【问题讨论】:
标签: c# wpf xaml dependency-properties