Expression Blend 绑定列表未列出用于绑定的接口属性
做了一个更简单的例子,希望有人可以遵循这个并帮助我
这是我的代码。
ViewModel
public class ViewModel
{
private Person _noninterfacePerson;
private IPerson _interfacePerson;
public ViewModel()
{
_noninterfacePerson = new Person();
_interfacePerson = new Person();
}
public Person NonInterfacePerson
{
get { return _noninterfacePerson; }
}
public IPerson InterfacePerson
{
get { return InterfacePerson; }
}
}
Person
public class Person : IPerson
{
public Person()
{
}
public string Name
{
get;
set;
}
public int age
{
get;
set;
}
}
IPerson
public interface IPerson
{
int age { get; set; }
string Name { get; set; }
}
View
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid d:DataContext="{d:DesignInstance local:ViewModel}">
</Grid>
</Window>
在 Expression Blend 中如果我插入文本块,请单击“高级选项”->数据绑定...->数据上下文 我将 InterfacePerson 和 NonInterfacePerson 视为要绑定的选项。但是,NonInterfacePerson 有一个小箭头,显示我可以绑定到的其他属性。在这种情况下,年龄和姓名。
当我将 d:DataContext 设置为 ad:DesignData Source 时,也会发生同样的情况。我没有在这个例子中使用它,因为它更复杂。但这就是我真正希望它发挥作用的地方,因为这样我就可以看到所有绑定选项并拥有示例数据。
如果我在我看来这样做:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ViewModel x:Key="DummyVM"/>
</Window.Resources>
<Grid d:DataContext="{Binding Source={StaticResource DummyVM}}">
</Grid>
</Window>
那么我可以看到 InterfacePerson 的属性,但是,我仍然无法使用 d:DesignData 轻松实现我想要的示例数据。
应该注意的是,在所有情况下,如果我手动输入路径,它就可以正常工作。这纯粹是让 Blend 显示它们的问题,以便更容易设置绑定。
感谢您对此提供的任何帮助!
Made a much simpler example, hopefully someone can follow this and help me
Here is my code.
ViewModel
public class ViewModel
{
private Person _noninterfacePerson;
private IPerson _interfacePerson;
public ViewModel()
{
_noninterfacePerson = new Person();
_interfacePerson = new Person();
}
public Person NonInterfacePerson
{
get { return _noninterfacePerson; }
}
public IPerson InterfacePerson
{
get { return InterfacePerson; }
}
}
Person
public class Person : IPerson
{
public Person()
{
}
public string Name
{
get;
set;
}
public int age
{
get;
set;
}
}
IPerson
public interface IPerson
{
int age { get; set; }
string Name { get; set; }
}
View
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid d:DataContext="{d:DesignInstance local:ViewModel}">
</Grid>
</Window>
In Expression Blend if I insert a textblock, click on the "Advanced Options" -> Data Binding... -> Data Context I see both InterfacePerson and NonInterfacePerson as options to bind to. However, NonInterfacePerson has a little arrow showing the other properties I can bind to. Age and Name in this case.
The same thing happens when I set d:DataContext to a d:DesignData Source. I didn't use that for this example because it is more complicated. But that is where I really want this to work because then I can see all my binding options AND have sample data.
If I instead do this in my view:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ViewModel x:Key="DummyVM"/>
</Window.Resources>
<Grid d:DataContext="{Binding Source={StaticResource DummyVM}}">
</Grid>
</Window>
Then I CAN see the properties of InterfacePerson, however, I still cant get the easy implementation of sample data that I would like using d:DesignData.
It should be noted that in all cases, if I manually type in the path it works fine. This is purely a matter of getting Blend to show them so it is easier to set up the bindings.
Thanks for any help you can provide on this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定他们使用反射来识别对象的属性,而接口只是布局的描述,而不是真实的对象,因此没有反射的属性。
希望这有帮助。
I am pretty sure they use reflection to identify the properties of an object and an Interface is only a description of layout, not a real object, so has no reflected properties.
Hope this helps.