2

私は4つの視覚的状態を定義しており、それぞれが同じシルバーライトコントロール内の異なる子コントロールに影響を与えています。これらの他の組み合わせを呼び出す他の視覚的状態を作成することは可能ですか?

したがって、Visual_Group_1、Visual_Group_2、Visual_Group_3、Visual_Group_4がある場合

  1. たとえば、Visual_Group_1とVisual_Group_3の状態を使用するVisual_Comb_1グループを作成することは可能ですか?
  2. 次に、Visual_Group_4とVisual_Group_3を使用するVisual_Comb_2という別のファイルを作成しますか?

を実装できてうれしいですsolution in xaml or codebehind or a combination of both。私が現在見ている代替案は、大量のコードのコピーと貼り付けを含み、私はそれを行うことにあまり熱心ではありません。

リクエストごとの詳細:

これは私が今大まかに持っているものです:

<VisualState x:Name="State1">
    <ColorAnimation Storyboard.TargetName="Path1"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="Blue" Duration="0:0:0.5" />
    // fade out the rest of the paths...
    <ColorAnimation Storyboard.TargetName="Path2"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="#00000000" Duration="0:0:0.5" />
    <ColorAnimation Storyboard.TargetName="Path3"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="#00000000" Duration="0:0:0.5" />
    <ColorAnimation Storyboard.TargetName="Path4"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="#00000000" Duration="0:0:0.5" />
</VisualState>

<VisualState x:Name="State2">
    <ColorAnimation Storyboard.TargetName="Path3"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="Red" Duration="0:0:0.5" />
    // fade out the rest of the paths...
    <ColorAnimation Storyboard.TargetName="Path2"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="#00000000" Duration="0:0:0.5" />
    <ColorAnimation Storyboard.TargetName="Path1"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="#00000000" Duration="0:0:0.5" />
    <ColorAnimation Storyboard.TargetName="Path4"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="#00000000" Duration="0:0:0.5" />
</VisualState>

<VisualState x:Name="State3">
    <ColorAnimation Storyboard.TargetName="Path4"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="Pink" Duration="0:0:0.5" />
    // fade out the rest of the paths...
    <ColorAnimation Storyboard.TargetName="Path2"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="#00000000" Duration="0:0:0.5" />
    <ColorAnimation Storyboard.TargetName="Path1"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="#00000000" Duration="0:0:0.5" />
    <ColorAnimation Storyboard.TargetName="Path3"
                    Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                    To="#00000000" Duration="0:0:0.5" />
</VisualState>

私の目的は、state1からstate3へのサイクルをクリックすると、各状態が別のパスでフェードアウトし、他のパスでフェードアウトするコントロールを用意することです。私の問題は、「残りのパスをフェードアウトする」セクションに大量のコピーと貼り付けがあることです。したがって、Path5を追加する場合は、すでに定義されているすべての視覚的状態に追加することを意味します。フェードオフの色やアニメーションを変更するには、すべての視覚的な状態に変更する必要があります。

4

1 に答える 1

4

XAMLを提供していただきありがとうございます。これが私が問題に取り組む方法です。

まず、VisualStatesそれぞれに対して個別に作成しますPathStyle(代わりに、各パスに非常によく似たものを再コーディングする手間を省くために使用することをお勧めしVisualStateますが、それぞれに異なる色を適用できるかどうかを知るには十分な知識がありません。)

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="Path1States">
        <VisualState x:Name="Activate">
            <Storyboard>
                <ColorAnimation Storyboard.TargetName="Path1"
                                Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                                To="Blue"
                                Duration="0:0:0.5" />
            </Storyboard>
        </VisualState>
        <VisualState x:Name="Deactivate">
            <Storyboard>
                <ColorAnimation Storyboard.TargetName="Path1"
                                Storyboard.TargetProperty="(Path.Fill).(SolidColorBrush.Color)"
                                To="#00000000"
                                Duration="0:0:0.5" />
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
    <VisualStateGroup x:Name="Path2States">
        <!-- ... etc ... -->
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

List次に、関連する各オブジェクトを含むコードビハインドでを作成し、GoToState1つのオブジェクトのin状態をオンにし、残りのオブジェクトのoff状態を呼び出すように独自の関数を修正します。

List<Path> pathList;

public Page() // constructor
{
    InitializeComponent();
    pathList = new List<Path>();
    pathList.Add(Path1);
    // and so forth
}

// Call this function when you want to change the state
private void ActivatePath(Path p)
{
    foreach (Path listItem in pathList)
    {
        // If the item from the list is the one you want to activate...
        if (listItem == p)
            VisualStateManager.GoToState(listItem, "Activate", true);
        // otherwise...
        else
            VisualStateManager.GoToState(listItem, "Deactivate", true);
    }
}

XAMLとスタイリングが得意であれば、VisualStatesを作成するためのよりクリーンな方法があるかもしれません。ただし、私の得意分野はロジックとコーディングの側面です。VisualStateそうは言っても、同じものを4〜5回書き出す方がはるかにクリーンです。:)
これがお役に立てば幸いです!

于 2011-05-31T08:15:05.223 に答える