【发布时间】:2015-12-12 15:14:57
【问题描述】:
public string City
{
get { return GetValue(CityProperty).ToString(); }
set { SetValue(CityProperty, value); }
}
public static readonly DependencyProperty CityProperty =
DependencyProperty.Register("City", typeof(string), typeof(Object1));
如果我犯了错误,请在此处: ...Register("城市"), typeof...
例如我忘记将“city”或“vity”大写
依赖属性子系统是否完全被吹走了?字符串 City 属性还在吗?我可以用各种方式设置它,静态的 CityProperty thingamabob 仍然存在,我可以用它在某个地方做点什么。我可以像 Object1.CityProperty 之类的那样做,但是故障/链接到底在哪里?如果 register 方法中的“City”文字和 City 属性不匹配,那么字符串 City 属性就不是依赖属性?
我想我的意思也是,如果字符串 City 属性调用 GetValue,那么有什么区别?子系统是否会“找到”所有内容并能够支持使用字符串 City 属性作为:a) 绑定目标 b) 动画 c) 样式
编辑
在一个有点令人不安的发展中,以下 'works' 。当您从 XAML 设置 City 属性时,它显然将其存储在某处。 a) 的哪些部分用作绑定目标、b) 样式、c) 动画对我来说是个谜。
namespace util
{
public class foo : FrameworkElement
{
public String City
{
get { return (String)GetValue(CityProperty); }
set { SetValue(CityProperty, value); }
}
// Using a DependencyProperty as the backing store for City. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CityProperty =
DependencyProperty.Register("city", typeof(String), typeof(foo), null);
}
}
namespace screwing_up_dependencies
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var aFoo = this.FindName("bar");
if (aFoo is util.foo)
{
util.foo theFoo = (util.foo)aFoo;
((Button)sender).Content = theFoo.City;
}
}
}
}
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"
xmlns:util="clr-namespace:util"
>
<util:foo x:Name="bar" City="blah"></util:foo>
<Button Content="Button" Grid.Row="1" Click="Button_Click" Height="105"/>
</Grid>
编辑 2 此外,您也可以这样做。注册命令的“名称”参数显然没有任何区别。而且 OwnerType 参数是什么似乎也无关紧要。
<util:foo x:Name="bar2" City="{Binding ElementName=ContentPanel, Path=Width}"></util:foo>
public static readonly DependencyProperty CityProperty =
DependencyProperty.Register(Guid.NewGuid().ToString(), typeof(String), typeof(object), null);
var aFoo = this.FindName("bar2");
if (aFoo is util.foo)
{
util.foo theFoo = (util.foo)aFoo;
((Button)sender).Content = theFoo.City;
}
【问题讨论】:
-
您是否尝试过任何方法来了解发生了什么?尝试在 XAML 中设置属性。
-
@Clemens 查看我的编辑。
标签: wpf xaml dependency-properties