Data Design Binding in WPF

I have a WPF window containing a ListBox. The ItemsSource element is bound to a property of the view model.

<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>

A view model property is an observable collection of user interfaces; ISomeInterface. The interface is very simple and implemented by SomeClass, which further overrides 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;
}

When I view a window in Visual Studio 2015 or Blend, everything is as expected. Called ToString and populated ListBox.

Hide screenshot

I created the XAML design data that I want to use in design mode. I added design data to the SampleData directory. I add the datacontext constructor to the XAML window right below the first DataContext.

d:DataContext="{d:DesignData Source=/SampleData/Data.xaml}"

. Visual Studio Blend " " , . /SampleData/Data.xaml, SampleData/Data.xaml,../SampleData/Data.xaml,./../SampleData/Data.xaml

Visual Studio Blend Data.xaml, SampleData . /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

jstreet . data.xaml , . , , .

ToString. List<object>, List<ISomeInterface>, object.ToString; . , , , ToString , , . , .

Visual Studio 2015.

+4
1

. - MSDN.

, , Data.xaml ( Dictionary1.xaml, ) VS:

enter image description here

, 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:

SampleData VS \ \WPF\ , . , .

<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>

SomeClasses: 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() :

enter image description here

+4

Source: https://habr.com/ru/post/1654183/


All Articles