6

構成ファイルを読み取り、それを使用してレポートを生成するための小さなアプリケーションを作成する必要があります。最終的にMVVMを使用したいと思っていましたが、開始するのはかなり難しいです。ああ、私はCaliburn.Microフレームワークを使用しています。

これが私が持っているもので、3つのボタンが付いたリボンを持つシェル(他のビューをホストするプライマリビュー)です。

1)ファイルを開く2)設定を表示する3)結果を表示する

また、他の2つのビュー、SettingsViewとResultsViewには、レポートを生成および削除するためのボタンがあります。

したがって、ビューの構造は次のようになると思います。

ShellView
   Ribbon
      OpenFileButton
      SettingsButton
      ResultsButton
   ContentControl (hosts SettingsView and ResultsView)

SettingsView
    CalculateResultsButton

ResultsView
    CancelResultsButton

トリッキーな部分はこれです:

1. "Show settings" button is disabled until a file is opened (via Open file). 
2. "Show results" button is disabled until a report is calculated (via a 
    method in SettingsViewModel).
3. If a report is calculated, the CalculateResultsButton is disabled and
   CancelResultsButton is enabled and vice versa.

どうすればこれを達成できるか教えてください。どうすればいいのかわからない。私のMVVMを考えていない頭脳は、ステータス変数を作成してから、それらのボタンをその変数にバインドする必要があると言っていますが、MVVMの世界では機能しないと思いますよね?コード例は非常にありがたいです!

どうもありがとう!

4

2 に答える 2

1

CMを使用しているので、コードビハインドは必要ありません。必要に応じて、.xaml.csファイルを削除できます。

これはかなり基本的な例ですが、ボタンの状態を制御する方法についてのアイデアが得られるはずです。この例でOpenは、が有効になり、他の2つは無効になります。をクリックするとOpenSettingsが有効になります。クリックすると同じことが起こりResultsます。Settings

グローバル状態を実行する方法が必要な場合は、シングルトンをSharedViewModelViewModelsに挿入することで同じ概念を適用でき、CanXXXメソッドはの値をチェックできますSharedViewModelこれはさまざまなもののSLデモですが、データを共有するためにシングルトンを注入しています。同じ考え方がwpfにも当てはまります。

ShellView:

<Window x:Class="CMWPFGuardSample.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Grid Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0"
                    Orientation="Horizontal">
            <Button x:Name="Open"
                    Content="Open" />
            <Button x:Name="Settings"
                    Content="Settings" />
            <Button x:Name="Results"
                    Content="Results" />
        </StackPanel>
    </Grid>

</Window>

ShellViewModel:

 [Export(typeof (IShell))]
    public class ShellViewModel : PropertyChangedBase, IShell
    {
        private bool _isOpen;
        public bool IsOpen
        {
            get { return _isOpen; }
            set
            {
                _isOpen = value;
                NotifyOfPropertyChange(() => IsOpen);
                NotifyOfPropertyChange(() => CanSettings);
            }
        }

        private bool _isSettings;
        public bool IsSettings
        {
            get { return _isSettings; }
            set
            {
                _isSettings = value;
                NotifyOfPropertyChange(() => IsSettings);
                NotifyOfPropertyChange(() => CanResults);
            }
        }

        public bool IsResults { get; set; }

        public void Open()
        {
            IsOpen = true;
        }

        public bool CanSettings
        {
            get { return IsOpen; }
        }

        public void Settings()
        {
            IsSettings = true;
        }

        public bool CanResults
        {
            get { return IsSettings; }
        }

        public void Results()
        {
        }
    }
于 2012-04-04T19:50:02.847 に答える
0

MVVM および WPF コマンドは、カスタム ロジックに基づいて対応するボタンを有効/無効にできるICommand.CanExecute()メソッドが組み込まれているため、「トリッキーな部分」の要件に完全に適合します。

この naice 機能を使用するには、最初にRoutedCommand クラスと、MSDN How to: Enable a Commandのわかりやすい例を参照してください (以下のコード スニペットを参照)。

そして、MVVM については、非常にシンプルです。EntityView.xaml試してみるだけで、それなしでは離れられません;)簡単に言えば、対応するクラスごとに作成しEntityViewModel、コードで明示的に、またはバインディングを使用して、ビューの DataContext にそのインスタンスを配置する必要があります。

var entityViewModel = new EntityViewModel();
var view = new EntityView();
view.DataContext = entityViewModel;

MVVM Command および Command.CanExecute バインディング:

XAML:

<Window x:Class="WCSamples.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CloseCommand"
    Name="RootWindow"
    >
  <Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Close"
                    Executed="CloseCommandHandler"
                    CanExecute="CanExecuteHandler"
                    />
  </Window.CommandBindings>
  <StackPanel Name="MainStackPanel">
    <Button Command="ApplicationCommands.Close" 
            Content="Close File" />
  </StackPanel>
</Window>

C# コード ビハインド:

// Create ui elements.
StackPanel CloseCmdStackPanel = new StackPanel();
Button CloseCmdButton = new Button();
CloseCmdStackPanel.Children.Add(CloseCmdButton);

// Set Button's properties.
CloseCmdButton.Content = "Close File";
CloseCmdButton.Command = ApplicationCommands.Close;

// Create the CommandBinding.
CommandBinding CloseCommandBinding = new CommandBinding(
    ApplicationCommands.Close, CloseCommandHandler, CanExecuteHandler);

// Add the CommandBinding to the root Window.
RootWindow.CommandBindings.Add(CloseCommandBinding);
于 2012-04-04T18:40:32.623 に答える