0

ViewModelクラスにブール変数があるとします。

public bool test = true;(これはC#です)

XAML/Expression Blend には、この変数を取り、PURELY XAML を使用して false に変更する方法はありますか?

マウスオーバーイベントでこれを行いたいです。マウスが特定のオブジェクトの上にある場合、ブール変数は false になり、それ以外の場合は true のままになります。

4

1 に答える 1

0

答え 1 (最も簡単):

なぜこれをしないのですか?

public bool Test
{
    get { return myControl.IsMouseOver; }
}

すべての XAML でそれを行いたいことはわかっていますが、既にプロパティを宣言しているので、言う代わりにこれを行うこともできます。

public bool Test = false;

回答 2 (より多くのコード、長期的にはより優れた MVVM アプローチ):

ここでは基本的に、Window1 に Dependency Property (Test と呼ばれます) を作成し、XAML 側で、その Test プロパティがボタンの IsMouseOver プロパティと同じになることを示す Window1 のスタイルを作成します (myButton_MouseEnter イベントを残したので、マウスがボタンの上にあるときに変数の状態を確認できます。自分で確認したところ、true に変わります。MouseEnter ハンドラーを削除しても、引き続き機能します)

XAML:

<Window x:Class="StackOverflowTests.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" x:Name="window1" Height="300" Width="300"
    xmlns:local="clr-namespace:StackOverflowTests">
    <Window.Resources>
        <Style TargetType="{x:Type local:Window1}">
            <Setter Property="Test" Value="{Binding ElementName=myButton, Path=IsMouseOver}">
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Button x:Name="myButton" Height="100" Width="100" MouseEnter="myButton_MouseEnter">
            Hover over me
        </Button>
    </Grid>
</Window>

C#:

public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        public bool Test
        {
            get { return (bool)GetValue(TestProperty); }
            set { SetValue(TestProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Test.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TestProperty =
            DependencyProperty.Register("Test", typeof(bool), typeof(Window1), new UIPropertyMetadata(false));

        private void myButton_MouseEnter(object sender, MouseEventArgs e)
        {
            bool check = this.Test;
        }
    }
于 2009-09-16T20:23:51.870 に答える