0

現在のプロジェクト ファイル内に、コントロールにストーリーボード アニメーションが適用されたユーザー コントロールがあります。ページでボタンがクリックされると、ストーリーボードが開始され、基本的にコントロールがユーザーに視覚的に表示されます。ストーリーボードは現在のページにリソースとして存在します

<navigation:Page.Resources>
    <Storyboard x:Name="PreferncesOpen">....</Storyboard x:Name="PreferncesOpen">
        </navigation:Page.Resources>

ページ内に、ストーリーボードを開始するクリックイベントがあるボタンがあります

private void btnOpenPreferences_Click(object sender, RoutedEventArgs e)
    {
        preferencesPanel.Visibility = System.Windows.Visibility.Visible;
        PreferncesOpen.Begin();
    }

userControl (preferencesPanel) 内に、クリックするとユーザー コントロールを閉じる/折りたたむ必要があるボタンがあります。Visibility.collapsed を使用してこれを行う予定です。ボタンはユーザーコントロール内にあるため、ルーティングされたコマンドを使用する必要があると思いますが、コントロールを含むページ内でアクションを呼び出す必要がありますか? 私はまだルーティングされたコマンドに慣れていないので、これが正しいアプローチだと思います。ユーザー コントロール内のボタンをクリックして、(このコントロールが存在する) ページの変更方法に影響を与えるコマンドを変更または実行する方法、またはその部分がページ内の他の要素に影響を与える方法がわかりません。たとえば、ユーザー コントロール内でボタンがクリックされたときに、ユーザー コントロールの可視性を折りたたみに設定したいと思います。また、メイン ページ内のグリッド列の 1 つの幅をサイズ変更したいと考えています。私は過去にページのコード ビハインドを使用してこれを行ったことがありますが、これの一部を分離しようとしています。ヒントをいただければ幸いです。

前もって感謝します

4

1 に答える 1

0

タイトルは少し誤解を招きます。私が正しく理解していれば、ルーティング イベントではなくコマンドについて質問しています。

DelegateCommand<T>Prism ライブラリの を使用する例を次に示します。それはたまたま私の個人的な好みです。

マークアップ :

<Button x:Name="MyButton" Content="Btn" Command="{Binding DoSomethingCommand}"/>

コード ビハインド* または ViewModel :

(* if you're not using MVVM make sure to add MyButton.DataContext = this; so you're sure that the button can databind to your code behind effectively)

public DelegateCommand<object> DoSomethingCommand
{
    get 
    { 
        if(mDoSomethingCommand == null)
            mDoSomethingCommand = new DelegateCommand(DoSomething, canDoSomething);
        return mDoSomethingCommand;
    }

private DelegateCommand<object> mDoSomethingCommand;

// here's where the command is actually executed
void DoSomething(object o)
{}

// here's where the check is made whether the command can actually be executed
// insert your own condition here
bool canDoSomething(object o)
{ return true; }


// here's how you can force the command to check whether it can be executed
// typically a reaction for a PropertyChanged event or whatever you like
DoSomethingCommand.RaiseCanExecuteChanged();

The argument that's passed to the above function is the CommandParameter dependency property (in Prism it's an attached property as well as the Command property if memory serves me right). When it's set, you can pass a value of your choosing to the command that you wish to execute.

Hope that helps.

于 2011-04-20T22:01:32.657 に答える