【发布时间】:2012-11-27 22:37:37
【问题描述】:
我正在为我的 Avalon 坞站控制器开发 DependencyProperty。这是我目前正在处理的一些示例代码。
要求是:在一个类中创建所有依赖属性,并在 View 中访问该属性。像这样的。
<Button isPaneVisible="true"> or <Button isPaneVisible="{Staticresource path=name, Mode=twoway">
您能帮我解决这个问题吗?
namespace Avatar.UI.ViewModel
{
internal class DependencyPropertyClass : DependencyObject
{
public static readonly DependencyProperty IsPaneVisibleProperty =
DependencyProperty.RegisterAttached("IsPaneVisible", typeof(bool), typeof(DependencyPropertyClass),
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsPaneVisible_PropertyChanged));
private static void IsPaneVisible_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
/// <summary>
/// Sets the IsPaneVisible for an element.
/// </summary>
public bool IsPaneVisible
{
get { return (bool)GetValue(IsPaneVisibleProperty); }
set
{
SetValue(IsPaneVisibleProperty, value);
}
}
}
}
<UserControl x:Class="Avatar.UI.View.ContentView"
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"
xmlns:avalonDock="http://avalondock.codeplex.com"
xmlns:local="clr-namespace:Avatar.UI.ViewModel"
d:DesignHeight="300" d:DesignWidth="300">
<Button IsPaneVisible="true"></Button
</UserControl>
【问题讨论】:
-
您要求我们帮助您解决问题,但您的问题中没有描述问题。请准确说明您的期望和得到的结果
-
似乎您正在尝试使用 DependencyProperty 作为附加属性,请参阅stackoverflow.com/questions/7148946/attached-properties 以了解说明
-
疯狂猜测:不要将类设为内部可能会导致 XAML 解析器在内部类上使用反射。
-
记得接受答案或发布您自己的答案。这将帮助您获得未来问题的答案(因为您的“接受”评级与您的用户 ID 一起发布)
标签: c# wpf dependencies