[英]Binding to design data in WPF
我有一个包含ListBox的WPF窗口。 ItemsSource绑定到视图模型的属性。
<Window x:Class="SimpleWpfApp.View.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"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding MainWindowViewModel, Source={StaticResource Locator}}">
<DockPanel>
<ListBox ItemsSource="{Binding SomeThings}" />
</DockPanel>
</Window>
视图模型的属性是自定义接口的可观察集合; ISomeInterface。 界面非常简单,由SomeClass实现,它还会覆盖ToString。
public class MainWindowViewModel
{
public ObservableCollection<ISomeInterface> SomeThings
{
get
{
var list = new List<ISomeInterface>
{
new SomeClass {Value = "initialised"},
new SomeClass {Value = "in"},
new SomeClass {Value = "code"}
};
return new ObservableCollection<ISomeInterface>(list);
}
}
}
public interface ISomeInterface
{
string Value { get; }
}
public class SomeClass : ISomeInterface
{
public string Value { get; set; }
public override string ToString() => Value;
}
当我在Visual Studio 2015或Blend中查看窗口时,所有内容都符合预期。 调用ToString并填充ListBox。
我创建了XAML设计数据,我想在设计模式下使用它。 我已将设计数据添加到名为SampleData的目录中。 我将设计datacontext语句添加到第一个DataContext正下方的窗口XAML中。
d:DataContext="{d:DesignData Source=/SampleData/Data.xaml}"
这不起作用。 无论我使用什么来源路径,Visual Studio和Blend报告'找不到文件或项目项'。 我试过/SampleData/Data.xaml,SampleData / Data.xaml,.. / SampleData / Data.xaml,。/。/ SampleData / Data.xaml
如果我将Data.xaml移出SampleData目录并进入项目根目录,Visual Studio和Blend只能找到Data.xaml。 然后我可以使用源路径/Data.xaml或Data.xaml来引用它。 如果我使用Data.xaml而没有前缀/然后Visual Studio和Blend报告无法找到该文件..但无论如何都要找到它。
我的第一个问题是..我可以在子目录中使用样本数据吗? 如果是这样怎么样?
在项目根目录中成功引用了Data.xaml之后,我的窗口没有调用重写的ToString,因此我得到了一个显示类名列表。 该列表与设计数据具有相同数量的项目,因此它看起来正在使用设计数据。
我的第二个问题是..为什么在从代码中实例化对象时,是否在这里调用被覆盖的ToString?
我知道我可以通过指定项目模板来实现所需的结果。
<ListBox ItemsSource="{Binding SomeThings}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
完整源代码可用于github上的示例应用程序
https://github.com/DangerousDarlow/WpfDesignData
UPDATE
感谢jstreet的回答。 我在子目录中更改了data.xaml的文件属性,现在我可以将其用作设计数据。 我以为我以前试过这个,但我一定是弄错了。
我还没有看到ToString被调用。 我尝试将视图模型属性更改为List<object>
以及List<ISomeInterface>
但两者都导致调用object.ToString; 通过显示类名推断出来。 我可能会停止看这一点,因为我不打算使用ToString,我将绑定到我想要显示的属性。 尽管如此,解释行为上的差异会更好。
我正在使用Visual Studio 2015社区版。
这是一些工作示例代码。 您可能需要参考本文 - MSDN 。
特别要注意如何在VS项目中设置Data.xaml
文件的属性(在我的例子中是Dictionary1.xaml ):
还要注意如何创建根对象SomeThings
(在我的例子中是SomeClasses ):
对于集合,根对象可以是ArrayList或从集合或泛型集合派生的自定义类型......
XAML:
<Window x:Class="WpfApplication277.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:WpfApplication277"
d:DataContext="{d:DesignData Source=/SampleData/Dictionary1.xaml}"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView ItemsSource="{Binding}"></ListView>
</Grid>
Dictionary1.xaml:
右键单击VS项目中的SampleData
文件夹,选择Add \\ New Item \\ WPF \\ Resource Dictionary ,将其内容替换为您的设计数据。 这应该确保您的设计数据可以位于子文件夹中。
<m:SomeClasses xmlns:m="clr-namespace:WpfApplication277">
<m:SomeClass Value="design data 1">
</m:SomeClass>
<m:SomeClass Value="design data 2">
</m:SomeClass>
<m:SomeClass Value="design data 3">
</m:SomeClass>
几类: List<SomeClass>
没有工作!
public class SomeClasses : List<Object>
{
public SomeClasses() { }
}
SomeClass的:
public class SomeClass : ISomeInterface
{
public string Value { get; set; }
public override string ToString() => string.Format("ToString() : {0}",Value);
}
请注意, ToString()
肯定被调用:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.