属性 Visual Studio 设计器中未显示更改
Property Change not showing in Visual Studio designer
我正在使用 VS2015 为小型应用程序的 TextBlock 添加一些自定义功能,并且由于我无法从 TextBlock 本身派生(它是密封的),所以我从 UserControl 派生。
在我的 xaml 文件中,我有
<TextBlock x:Name="innerText"/>
作为用户控件中的唯一元素。
在我的代码隐藏中,我使用以下代码访问文本:
public string Label
{
get { return innerText.Text; }
set {
if (value != innerText.Text)
{
innerText.Text = value;
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs("Label"));
}
}
}
当我运行安装我的应用程序时,这非常有用。在其他页面上,我能够添加控件的实例并正确设置 "Label" 属性。不幸的是,"Label" 属性 的值不会传递到设计器本身的内部文本框。
如何在设计器中获取要更新的值?虽然不是绝对必要的(正如我所说,在 运行 时它工作正常),但它会使设计器中的布局对我来说更容易。
更新:
我也尝试使用 DependencyProperty,但有同样的问题。 运行-time 效果很好,design-time 什么也没显示。
public string Label
{
get { return GetValue(LabelProperty).ToString(); ; }
set { SetValue(LabelProperty, value); }
}
public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(AutoSizingText), new PropertyMetadata(string.Empty));
然后,在 xaml 中,我为整个控件设置了 DataContext:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
并尝试绑定文本值:
<TextBlock Text="{Binding Label}" />
我建议使用 dependency property 而不是依赖设置 innerText
元素的 Text
属性。依赖项 属性 的行为与控件上的任何其他 属性 一样,包括在设计模式下更新。
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
// Using a DependencyProperty as the backing store for Label. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LabelProperty =
DependencyProperty.Register("Label", typeof(string), typeof(MyClassName), new PropertyMetadata(string.Empty));
您的 XAML 将如下所示:
<UserControl x:Name="usr" ...>
...
<TextBlock Text="{Binding Label, ElementName=usr}" ... />
...
</UserControl>
专业提示:键入 propdp
,然后输入 Tab, Tab
以快速创建依赖项 属性.
这是一个用法示例:
<local:MyUserControl Label="Le toucan has arrived"/>
注意:在使用依赖项属性时不需要将DataContext
设置为Self
,这通常会搞砸作为 UserControl
不应该设置它自己的 DataContext
,父控件应该设置。
我正在使用 VS2015 为小型应用程序的 TextBlock 添加一些自定义功能,并且由于我无法从 TextBlock 本身派生(它是密封的),所以我从 UserControl 派生。
在我的 xaml 文件中,我有
<TextBlock x:Name="innerText"/>
作为用户控件中的唯一元素。
在我的代码隐藏中,我使用以下代码访问文本:
public string Label
{
get { return innerText.Text; }
set {
if (value != innerText.Text)
{
innerText.Text = value;
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs("Label"));
}
}
}
当我运行安装我的应用程序时,这非常有用。在其他页面上,我能够添加控件的实例并正确设置 "Label" 属性。不幸的是,"Label" 属性 的值不会传递到设计器本身的内部文本框。
如何在设计器中获取要更新的值?虽然不是绝对必要的(正如我所说,在 运行 时它工作正常),但它会使设计器中的布局对我来说更容易。
更新: 我也尝试使用 DependencyProperty,但有同样的问题。 运行-time 效果很好,design-time 什么也没显示。
public string Label
{
get { return GetValue(LabelProperty).ToString(); ; }
set { SetValue(LabelProperty, value); }
}
public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(AutoSizingText), new PropertyMetadata(string.Empty));
然后,在 xaml 中,我为整个控件设置了 DataContext:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
并尝试绑定文本值:
<TextBlock Text="{Binding Label}" />
我建议使用 dependency property 而不是依赖设置 innerText
元素的 Text
属性。依赖项 属性 的行为与控件上的任何其他 属性 一样,包括在设计模式下更新。
public string Label
{
get { return (string)GetValue(LabelProperty); }
set { SetValue(LabelProperty, value); }
}
// Using a DependencyProperty as the backing store for Label. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LabelProperty =
DependencyProperty.Register("Label", typeof(string), typeof(MyClassName), new PropertyMetadata(string.Empty));
您的 XAML 将如下所示:
<UserControl x:Name="usr" ...>
...
<TextBlock Text="{Binding Label, ElementName=usr}" ... />
...
</UserControl>
专业提示:键入 propdp
,然后输入 Tab, Tab
以快速创建依赖项 属性.
这是一个用法示例:
<local:MyUserControl Label="Le toucan has arrived"/>
注意:在使用依赖项属性时不需要将DataContext
设置为Self
,这通常会搞砸作为 UserControl
不应该设置它自己的 DataContext
,父控件应该设置。