2017-06-09 11 views
1

私はWPFを初めて使ったことがありますが、今月1ヶ月間働いていて、自分のUIが間違っているかもしれないことに気付きました。ここでwpfでXAMLを再利用しますか?

はシナリオです:私は、次のような特定のコンテナがあります。

<DockPanel> 
    <WrapPanel> 
     ... some buttons here which are always the same. 
    <WrapPanel> 
    <Grid name="myGrid" ...contains some attributes> 
     // Everything inside here is generated dynamically. 
    <Grid> 
</DockPanel> 

を私の質問は、私は、このような状況に対処はどうすればよいのですか?私はそれを再利用できるように何らかの種類のテンプレートを作成したいと思います。テンプレートは上記の構造全体でなければなりません。今、私はコードの中で、過度の種類のように見える "全体"部分を作成しています。だから、私はこれらのDockpanelsの多くを作成することができますが、私のグリッドの中には違う唯一のコンテンツがあります。

私はテンプレートとして使用できるこのタスク(私はそれをUIモジュールと呼ぶ)用に別個のXML文書を定義したいと思います。擬似コードで達成しようとするものイムの

例::だからコードで、私は私のような何かができる望んでいるだろうあなたは、単にUIあなたの作品を再利用できるようにしたい場合は

var panel = createNewControlFrom("/MySpecialDockPanel.xaml"); 
panel.Background = // Different color. 
var panel.Children.FindName("myGrid"); 

loop start 
    // Add children to panels grid. 
loop end 
+2

ユーザーコントロールを作成しますか? – mm8

+0

@ mm8非常に多くの情報が失われています。データ型、リソース、ユーザコントロール。 Iveは最近読んでいます:https://stackoverflow.com/questions/1803422/what-is-the-best-way-to-reuse-blocks-of-xaml – Asperger

+0

私が現在やっていることはプログラム的ですが、私はXAMLに入ることができます。 – Asperger

答えて

3

UserControlを作成し、この中に再利用可能なXAMLマークアップと任意のコードを定義することができます。

あなたは、どちらのプログラムで、このUserControlの任意の数のインスタンスを作成することができます。

UserControl1 uc = new UserControl1(); 

...または別のクラスのXAMLマークアップでは、例えば窓:

<local:UserControl1 /> 
+1

あなたは用語に精通していないときは、いつも少し難しいです。私はgreateの記事を見つけました:http://blog.scottlogic.com/2012/02/06/a-simple-pattern-for-creating-re-useable-usercontrols-in-wpf-silverlight.html – Asperger

+0

ありがとう。 – Asperger

2

WPFで使用されているものの内訳は次のとおりです。
DataTemplate
これは特定のタイプのコレクションで使用するので、あなたは、クラスを持っていると言うされています

class ModelItem //Implements INotifyPropertyChanged 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
} 

これはあなたのViewModelでコレクションのようになります。

public class MainViewModel //Implements INotifyPropertyChanged 
{ 
    List<ModelItem> Items { get; set; } 
} 

次に、あなたのXAMLでそんなにのようにそれを使用します。

<UserControl x:Class="SO_app.ForAsperger" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:SO_app" 
     xmlns:vm="clr-namespace:VM;assembly=VM"//this is where we define reference to our ViewModel 
     xmlns:model="clr-namespace:Model;assembly=Model"//this is where we define our model object so we know the structure for our DataTemplate 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<UserControl.DataContext> 
    <vm:MainViewModel/>//Here we assign the ViewModel to our View 
</UserControl.DataContext> 
<UserControl.Resources>//this is where you can place Collections, Styles, Controls etc ... 
    <DataTemplate x:Key="keyToReferToInXaml"> 
     <TextBlock Text="SomeText"/> 
    </DataTemplate> 
<CollectionViewSource Source="{Binding Items}" x:Key="items"/>//this is a collection of the same items but held as a StaticResource which you can then reference like <ItemsControl ItemsSource="{Binding Source={StaticResource items}}"/> 
</UserControl.Resources> 
<ItemsControl ItemsSource="{Binding Items}">//This is our List, you can use ListView or ListBox if this seems intimidating 
    <ItemsControl.ItemTemplate> 
     <DataTemplate DataType="{x:Type model:ModelItem}">//We define the target type for this DataTemplate 
      <TextBlock Text="{Binding Name}"/>//Binding will pick up the properties from ModelItem and display them in a TextBlock 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

関連する問題