1

Caliburn.MicroでWPFビューを使用して再利用する方法はありますか?

たとえば、[保存して閉じる]ボタンと[キャンセル]ボタンが付いたDetailViewBase.xamlがあります。ここで、PersonView(Model)でDetailViewModelBaseとXAMLを使用します。

これが明確な質問であることを願っています。

4

2 に答える 2

2

私の仮定が正しければ、あなたが意味するのは、いくつかのより単純なビューモデルからより複雑なビューを「構成」したいということだと思います。ビューモデルから継承できますが、明らかに、ビューモデルは一度に1つのアクティブなビューしか持つことができません。

ただし、バインディングを使用すると、複数のビューモデルを作成し、それらすべてでそれぞれのビューを一緒にレンダリングして、より複雑なUIを作成できます。

例えば

保存/キャンセルボタンを備えたVM

public class ActionsViewModel
{
    public void Save()
    {
    }

    public void Cancel()
    {
    }
}

対応するビュー:

<UserControl>
   <StackPanel Orientation="Horizontal">
       <Button x:Name="Save">Save</Button>
       <Button x:Name="Cancel">Cancel</Button>
   </StackPanel>
</UserControl>

それ自体とを構成する別のビューActionsViewModel

public class ComposedViewModel
{
    public ActionsViewModel ActionsView
    {
        get; set;
    }

    public ComposedViewModel()
    {
        ActionsView = new ActionsViewModel();
    }      

    public void DoSomething()
    {
    }
}

景色:

<UserControl>
   <StackPanel Orientation="Horizontal">
       <TextBlock>Hello World</TextBlock>
       <Button x:Name="DoSomething">Do Something</Button>
       <ContentControl x:Name="ActionsView" />
   </StackPanel>
</UserControl>

コンベンションバインディングを使用する(または添付されたプロパティを使用する)と、ContentControlCMは自動的にVMをにバインドDataContextし、ビューを接続します。このようにして、複数のVMを1つのより複雑な機能に構成できます。

さらに、アクションメッセージがバブリングするため、の「OK」と「キャンセル」の実装を削除して実装にActionsViewModel配置すると、アクションメッセージがインスタンスComposedViewModelにバブルアップし、代わりにそこでメソッドが起動されます。ComposedViewModel

例えば

public class ActionsViewModel
{
    // Remove the command handlers
}


public class ComposedViewModel
{
    public ActionsViewModel ActionsView
    {
        get; set;
    }

    public ComposedViewModel()
    {
        ActionsView = new ActionsViewModel();
    }      

    public void DoSomething()
    {
    }

    // CM will bubble the messages up so that these methods handle them when the user clicks on the buttons in the ActionsView
    public void Save()
    {
    }

    public void Cancel()
    {
    }
}

編集:

申し訳ありませんが、これを忘れてしまいました。コンベンションベースのバインディングではメッセージをバブルできませんが、Message.Attach添付のプロパティを使用してこれを機能させることができます。

// Make sure you include the caliburn namespace:
<UserControl xmlns:cal="http://www.caliburnproject.org">
   <StackPanel Orientation="Horizontal">
       <Button x:Name="Save" cal:Message.Attach="Save">Save</Button>
       <Button x:Name="Cancel" cal:Message.Attach="Cancel">Cancel</Button>
   </StackPanel>
</UserControl>
于 2013-03-11T21:24:23.030 に答える
0

次のようなコードを使用して、ViewModelをViewに明示的にバインドできます。

cal:Bind.Model={Binding DetailViewModelBase}
于 2013-03-11T20:37:45.780 に答える