簡體   English   中英

綁定到WPF中的設計數據

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM