- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试制作一个具有可绑定(bind)属性的示例 WPF 用户控件(也许说“开发人员控件”会更好)。我的代码由这些文件组成:
----- MainWindow.xaml -----
<Window x:Class="Test_Binding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:testBinding="clr-namespace:Test_Binding"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<testBinding:MyLabelledTextBox x:Name="MLTB" LabelText="My custom control: MyLabelledTextBox" Text="{Binding StringData, Mode=OneWay}" />
</StackPanel>
</Window>
----- MainWindow.xaml.cs -----
using System.Windows;
namespace Test_Binding
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
this.DataContext = new MyDataObject();
this.InitializeComponent();
}
}
}
----- MyDataObject.cs -----
using System.Runtime.CompilerServices; // CallerMemberName
using System.ComponentModel; // INotifyPropertyChanged
namespace Test_Binding
{
public class MyDataObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string stringData;
public string StringData
{
get { return this.stringData; }
set
{
if (value != this.stringData)
{
this.stringData = value;
this.OnPropertyChanged();
}
}
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public MyDataObject()
{
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 10000;
t.Elapsed += t_Elapsed;
t.Start();
}
private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.StringData = ((this.StringData ?? string.Empty).Length >= 4 ? string.Empty : this.StringData + "*");
}
}
}
----- MyLabelledTextBox.xaml -----
<UserControl x:Class="Test_Binding.MyLabelledTextBox"
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"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Background="Yellow">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*" />
<ColumnDefinition Width="0.5*" />
</Grid.ColumnDefinitions>
<Label x:Name="MLTBLabel" Grid.Row="0" Grid.Column="0" />
<TextBox x:Name="MLTBTextBox" Grid.Row="0" Grid.Column="1" Background="Yellow" Text="{Binding Text, Mode=TwoWay}" />
</Grid>
</StackPanel>
</UserControl>
----- MyLabelledTextBox.xaml.cs -----
using System.Windows;
using System.Windows.Controls;
namespace Test_Binding
{
/// <summary>
/// Interaction logic for MyLabelledTextBox.xaml
/// </summary>
public partial class MyLabelledTextBox : UserControl
{
public static readonly DependencyProperty LabelTextProperty =
DependencyProperty.Register("LabelText", typeof(string), typeof(MyLabelledTextBox),
new PropertyMetadata(string.Empty, MyLabelledTextBox.LabelTextPropertyChanged));
public string LabelText
{
get { return (string)this.GetValue(MyLabelledTextBox.LabelTextProperty); }
set { this.SetValue(MyLabelledTextBox.LabelTextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MyLabelledTextBox),
new PropertyMetadata(string.Empty, MyLabelledTextBox.TextPropertyChanged));
public string Text
{
get { return (string)this.GetValue(MyLabelledTextBox.TextProperty); }
set { this.SetValue(MyLabelledTextBox.TextProperty, value); }
}
public MyLabelledTextBox()
{
this.InitializeComponent();
this.MLTBLabel.DataContext = this;
this.MLTBTextBox.DataContext = this;
this.MLTBTextBox.TextChanged += new TextChangedEventHandler(this.MLTBTextBox_TextChanged);
}
private void MLTBTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
this.Text = this.MLTBTextBox.Text; // transfer changes from TextBox to bindable property (bindable property change notification will be fired)
}
private static void LabelTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((MyLabelledTextBox)d).MLTBLabel.Content = (string)e.NewValue; // transfer changes from bindable property to Label
}
private static void TextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((MyLabelledTextBox)d).MLTBTextBox.Text = (string)e.NewValue; // transfer changes from bindable property to TextBox
}
}
}
有一个“MyDataObject”类的实例,其属性为“StringData”,该实例使用计时器定期修改。我的用户控件绑定(bind)到它的属性“StringData”。如果“MainWindow.xaml”文件中的绑定(bind)设置为“TwoWay”,用户控件会不断更新,但如果我使用“OneWay”绑定(bind),则用户控件会更新一次,然后“PropertyChanged” “MyDataObject”类实例的事件不会再次触发,因为它突然没有订阅者。
为什么“OneWay”绑定(bind)在调用一次后就停止工作了?什么代码更改可以让“TwoWay”和“OneWay”绑定(bind)继续工作?
最佳答案
首先。
this.MLTBLabel.DataContext = this;
this.MLTBTextBox.DataContext = this;
不不不不!
从来没有。曾经。曾经。从代码隐藏设置您的 DataContext
。一旦这样做,您就失去了从父控件绑定(bind)到用户控件的依赖属性的神奇之美。换句话说,就是不要这样做。
这是你应该做的:
给你的 UserControl
一个 x:Name
。
<UserControl ...
x:Name="usr">
将您的 UserControl 的依赖属性绑定(bind)到您的元素,如下所示:
<TextBlock Text="{Binding MyDependencyProperty, ElementName=usr}" ... />
将您的 UserControl 的 DataContext 属性绑定(bind)到您的元素,如下所示:
<TextBlock Text="{Binding MyDataContextProperty}"/>
使用此方法将允许您在 MainWindow
中设置 UserControl
的 DataContext
,但仍然能够绑定(bind)到 UserControl 的UserControl 中的依赖属性。如果您在代码隐藏中设置 UserControl 的 DataContext,您将无法绑定(bind)到您的依赖属性。
现在,进入您的实际问题。
所有这些:
private void MLTBTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
this.Text = this.MLTBTextBox.Text; // transfer changes from TextBox to bindable property (bindable property change notification will be fired)
}
private static void LabelTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((MyLabelledTextBox)d).MLTBLabel.Content = (string)e.NewValue; // transfer changes from bindable property to Label
}
private static void TextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((MyLabelledTextBox)d).MLTBTextBox.Text = (string)e.NewValue; // transfer changes from bindable property to TextBox
}
算了吧。看来您正试图规避我之前谈到的不当行为。
你应该绑定(bind)到你的依赖属性:
<Label Grid.Row="0" Grid.Column="0" Text="{Binding Text, ElementName=usr}"/>
您遇到的另一个问题是,在您的 MainWindow
中,您在 UserControl
上使用了绑定(bind)。
Text="{Binding StringData, Mode=OneWay}"
现在,因为您已经在代码隐藏中设置了 DataContext
。这实际上是在说:
从当前控件的 DataContext 绑定(bind)到 StringData。
在您的情况下,这是与您的 MainWindow
DataContext 完全不同的绑定(bind)。 (因为您已经在 UserControl 中明确设置了 DataContext)。
运行我之前提到的内容。有很多东西要学,但这只是一个开始。
关于c# - WPF UserControl with Binding Mode=OneWay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30560390/
我正在开发基于桌面 (Windows 7) 的应用程序,并使用 Qt Creator v 5.6.0 开发程序。我有一个非常奇怪的问题,即 我的程序在 DEBUG 模式下崩溃,但在 RELEASE 模
我爱org-tables ,我用它们来记录各种事情。我现在正在为 Nix 记录一些单行代码(在阅读了 Domen Kožar 的 excellent guide 后,在 this year's Eur
org-mode 是否有一个键绑定(bind)可以在编号/项目符号列表项之间移动,就像您可以对标题一样? 喜欢的功能: org-forward-heading-same-level 大纲下一个可见标题
我知道这有点小,但它一直困扰着我。我正在为一个项目使用 Org-mode,我倾向于经常导出为 PDF 或 HTML,这使我的目录中散落着 PDF、Tex 和 HTML 文件。有没有办法将 Org 模式
有什么方法可以让 org-mode 继续编号列表而不是重新启动? 这是情况。你列了一个类似的列表: Sometimes you can restart the display by doing som
如何在组织模式文件中生成所有标签的枚举列表(例如 :tag:)?假设我有一个以下形式的列表: * Head1 :foo:bar: ** Subhead1 :foo: * Head2
我正在使用 org-mode(Emacs:24.3.1,org-mode: 7.9.3f 8.0.6)作为不同代码片段的数据库语言(目前主要是 elisp 和 python)。这在使用 org-mod
相关问题:org-mode: fontify code blocks natively 截至 2012 年 11 月 1 日,我已经获得了最新的 org-mode 和 emacs 版本(组织存储在 o
有谁知道在使用 ido 模式时区分 dired 模式缓冲区名称与迷你缓冲区中其他类型缓冲区的好方法吗?例如...在 dired 模式缓冲区名称末尾显示正斜杠? 最佳答案 您可以简单地更改dired-m
在这个示例脚本中 import argparse parser = argparse.ArgumentParser() parser.add_argument('--modes', help="tes
我第一次学习“操作系统”。在我的书中,我发现了关于“用户模式”和“内核模式”的这句话: "Switch from user to kernel mode" instruction is execute
我刚刚下载了 Processing 2.0 并尝试从“模式管理器”安装 Android 模式。但是在安装时出现错误提示:“无法将模式“Android 模式”移动到速写本”。我怎样才能摆脱这个错误? 最
在 android L 中,我尝试将相机闪光灯模式设置为 TORCH,它工作正常,但我无法将其更改回闪光灯模式 AUTO 或闪光灯模式 打开。我只能返回闪存模式 OFF。我尝试了像 camera360
有 2 台机器,A 和 B。有 2 个分支,p16 和 c2。 A 有一个 ext3 文件系统,但在 B 上,存档位于带有 vfat 的 truecrypt 驱动器上,mount 显示 rw,uid=
我有 linum-mode在我的 Emacs 配置中全局启用。全局启用意味着它也适用于不受欢迎的速度条。 我为这个问题找到的唯一建议是在存档的 Emacs 帮助邮件列表中,它建议以下 speedbar
Google Cloud Firestore 将很快取代旧的 Google Cloud Datastore。然后可以选择在“ native 模式”或“数据存储模式”下使用 Cloud Firestor
org-mode的版本我的版本 Emacs 附带的(24.5.2) 是 8.2.10 .我已安装版本 8.3.1从 ELPA 并将其添加到我的 init 文件中: (add-to-list 'load
The org-mode manual指出 org-mode 将“”“...在 shell 链接”“”中执行命令,但它不显示此类链接的语法。 我希望能有一个简单完整的示例来说明这种 shell 链接是
我正在尝试在 emacs 24 中使用 dart 模式和 d 模式。如果我单独使用任何一种模式,一切都很好。如果我分别对每种类型的文件使用这两种模式,我在尝试缩进 D 代码时会出错。 以下是在初始化时
我的应用程序中有 CupertinoDatePicker 以使用以下代码选择日期和时间: formatColumn( widget: Consumer( builder: (_, mcProvide
我是一名优秀的程序员,十分优秀!