c# - 将 Label 的 target 属性设置为自定义 UserControl 中的对象
问题描述
只有标签和自定义控件的简单窗口。当Path=ValueContainer
包含并使用访问键时,它会发出 Windows 通知声音,但焦点不会放在文本框中。使用 F12(转到定义)可以工作并导航到文本框。
如果Path=ValueContainer
省略,则不会发出声音并且焦点不会改变。
<Window x:Class="DebuggingSearchBoxAccessKey.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DebuggingAccessKey"
Title="MainWindow" Height="200" Width="200">
<StackPanel Orientation="Horizontal">
<Label Content="_Testing"
Target="{Binding ElementName=searchBox, Path=ValueContainer}" />
<local:SearchBox x:Name="searchBox" />
</StackPanel>
</Window>
TextBox
包含我希望访问密钥绑定到的自定义控件。
<UserControl x:Class="DebuggingAccessKey.SearchBox"
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:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:DebuggingAccessKey"
mc:Ignorable="d"
d:DesignHeight="20" d:DesignWidth="100">
<Grid>
<TextBox x:Name="ValueContainer"
Text="Bazzle"/>
</Grid>
</UserControl>
Text
据我了解,我还将该属性实现为DependencyProperty
.
public partial class SearchBox : UserControl
{
public SearchBox()
{
InitializeComponent();
}
public static readonly DependencyProperty TextDependencyProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(SearchBox));
public string Text
{
get => (string)GetValue(TextDependencyProperty);
set => SetValue(TextDependencyProperty, value);
}
}
绑定到标准文本框有效,如下所示。因此,我猜我错过了自定义控件所需的明显内容......
<StackPanel Orientation="Horizontal">
<Label Content="_Foo" Target="{Binding ElementName=bar}" />
<TextBox x:Name="bar" />
</StackPanel>
解决方案
只能为 PROPERTIES 创建绑定。但是嵌套在 UserControl 中的 UI 元素是 FIELDS。
尝试添加属性。
XAML:
<UserControl x:Class="DebuggingAccessKey.SearchBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="20" d:DesignWidth="100">
<Grid>
<TextBox x:Name="PART_ValueContainer"
Text="Bazzle"/>
</Grid>
</UserControl>
代码背后:
using System.Windows;
using System.Windows.Controls;
namespace DebuggingAccessKey
{
/// <summary>Interaction logic for SearchBox.xaml</summary>
public partial class SearchBox : UserControl
{
public SearchBox()
{
InitializeComponent();
ValueContainer = PART_ValueContainer;
}
public TextBox ValueContainer
{
get { return (TextBox)GetValue(ValueContainerProperty); }
private set { SetValue(ValueContainerPropertyKey, value); }
}
// Use DependencyPropertyKey to create a read-only DependencyProperty ValueContainerProperty.
private static readonly DependencyPropertyKey ValueContainerPropertyKey =
DependencyProperty.RegisterReadOnly(nameof(ValueContainer), typeof(TextBox), typeof(SearchBox), new PropertyMetadata(null));
// Using a DependencyProperty as the backing store for ValueContainer. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueContainerProperty = ValueContainerPropertyKey.DependencyProperty;
}
}
推荐阅读
- javascript - 是否需要在头部包含一些字体或者它们是默认的?
- c# - 带有 NetTopologySuite.IO.SqlServerBytes 的 SqlDataAdapter、SqlBulkCopy 和 DataTable
- javascript - 用jquery暂停视频
- c++ - 在 C++ 中使用 Apache Arrow 打开 CSV 文件
- python - Conda环境中的Tensorflow安装错误
- excel - 查看文件夹/子文件夹以查找和打印 pdf 列表
- twilio - Twilio 可编程视频 - 无需用户输入房间 ID 和名称即可开始视频通话
- git - 保留最后 X 次提交和历史记录
- python - 如何使用 random.random() 生成 30 到 35 之间的伪随机实数?
- javascript - 从服务器到客户端的 Json 数据传输