简体   繁体   English

如何直接在WPF中使用自定义依赖项属性?

[英]How to use Custom Dependency Properties directly in WPF?

Edit : I changed the code according to Thorstens Answer, using the enum, but did not work. 编辑 :我根据Thorstens答案使用枚举更改了代码,但没有用。

I am using Dependency Properties to influence a WPF control I am creating. 我正在使用“依赖项属性”来影响我正在创建的WPF控件。 I'm new to WPF, so I'm not sure what I am doing wrong and I can't find proper articles explaining it. 我是WPF的新手,所以我不确定自己做错了什么,也找不到合适的文章对其进行解释。

For example, I'm trying to define the Visibility of a control via Dep Properties. 例如,我试图通过Dep Properties定义控件的可见性。 The property, in this case, would be this: 在这种情况下,该属性为:

public static readonly DependencyProperty IconVisibilityBoldProperty = 
DependencyProperty.Register("IconVisibilityBold", typeof(Visibility), typeof(RTFBox),
new PropertyMetadata(Visibility.Hidden), VisibilityValidateCallback);
private static bool VisibilityValidateCallback(object value)
{
 Visibility prop = (Visibility) value;
 if (prop == Visibility.Hidden || prop == Visibility.Visible)
 {
  return true;
 }
 return false;
}
public Visibility IconVisibilityBold
{
 get
 {
  return (Visibility)GetValue(IconVisibilityBoldProperty);
 }
 set
 {
  SetValue(IconVisibilityBoldProperty, value);
 }
}

Edit: for correct XAML, look for Slugarts answer. 编辑:对于正确的XAML,寻找Slugarts答案。 The XAML Entry for this, in this case a ToggleButton, would be 为此的XAML项(在本例中为ToggleButton)将是

<ToggleButton Visibility="{Binding Path=IconVisibilityBold}" ToolBar.OverflowMode="Never" x:Name="ToolStripButtonBold" Command="EditingCommands.ToggleBold" ToolTip="Bold">
<Image Source="Images\Bold.png" Stretch="None"/>
</ToggleButton>

I've output the Property, it shows as "Hidden" as the Metadata Default Value should imply, but apparently I've done something wrong with the binding. 我已经输出了Property,它显示为“ Hidden”,这是因为Metadata Default Value应该暗示,但是显然我在绑定方面做错了。 What would I have to write there? 我要在那里写什么?

You are trying to binding to a property of the parent control without referencing it, and it won't be set implicitly. 您试图绑定到父控件的属性而不引用它,并且不会隐式设置它。 You need to set the ElementName in the ToggleButton binding to be the name of the UserControl you are creating (giving it an x:Name property if it doesn't have one already). 您需要将ToggleButton绑定中的ElementName设置为您正在创建的UserControl的名称(如果尚未提供,则为其提供x:Name属性)。

<UserControl x:Name="rtfBox">
<ToggleButton Visibility="{Binding ElementName=rtfBox, Path=IconVisibilityBold}" ... />
...
</UserControl>

Also you should follow the previous answers which correctly state that the Visibility property is an enum and not a string. 另外,您应该遵循前面的答案,正确地表明Visibility属性是枚举而不是字符串。

So your property is a string...but it has to be a enumerable: 因此,您的属性是一个字符串...但是它必须是可枚举的:

namespace System.Windows
{
    public enum Visibility : byte
    {
        Visible,
        Hidden,
        Collapsed,
    }
}

You have to bind textbox the datacontext or use it as reference to access the property correctly 您必须将文本框绑定到datacontext或将其用作引用以正确访问属性

The ToggleButton 's Visibility property requires a value of type System.Windows.Visibility . ToggleButtonVisibility属性需要一个System.Windows.Visibility类型的值。 You need to change your code to use that instead of strings: 您需要更改代码以使用它而不是字符串:

public static readonly DependencyProperty IconVisibilityBoldProperty = 
DependencyProperty.Register("IconVisibilityBold", typeof(System.Windows.Visibility), typeof(RTFBox));

public System.Windows.Visibility IconVisibilityBold
{
 get
 {
  return (System.Windows.Visibility)GetValue(IconVisibilityBoldProperty);
 }
 set
 {
  SetValue(IconVisibilityBoldProperty, value);
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM