2

プロパティの値が変更されたときにコントロールの外観を変更しようとしています。以下のXAMLおよびC#コードを使用していますが、最初にアプリケーションを実行したときを除いて、プロパティmyPropertyの値を変更するボタンをクリックしても何も起こりません。助言がありますか?

 <Window x:Class="WpfApplication3.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <Button Height="34" HorizontalAlignment="Left" x:Name="toggleBroadcast" VerticalAlignment="Top" Width="133" Margin="180,0,0,0">
            <Button.Style>
                <Style x:Name="bb" TargetType="{x:Type Button}">
                    <Setter Property="Content" Value="Original Content"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=myProperty}" Value="true">
                            <Setter Property="Content" Value="IS TRUE"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=myProperty}" Value="false">
                            <Setter Property="Content" Value="IS FALSE"/>
                        </DataTrigger>
                    </Style.Triggers>

                </Style>
            </Button.Style>
         </Button>
        <Button Content="Click on This Button" HorizontalAlignment="Left" Width="158" Click="Button_Click_1" Height="34" VerticalAlignment="Top"/>
   </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    public partial class MainWindow : Window
    {
        Boolean _myProperty = false;
        public MainWindow()
        {
            DataContext = this;
            InitializeComponent();
        }
        public Boolean myProperty
        {
            get
            {
                return _myProperty;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            _myProperty = !_myProperty;
        }
    }
}
4

1 に答える 1

3

を使用していない場合は、プロパティが変更されたことを Xaml に通知できるようDependencyPropertyに実装する必要があります。INotifyPropertyChanged

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private bool _myProperty = false;
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }


    public bool myProperty
    {
        get { return _myProperty; }
        set { _myProperty = value; NotifyPropertyChanged("myProperty"); }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        myProperty = !myProperty;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// Notifies the property changed.
    /// </summary>
    /// <param name="property">The property name that changed.</param>
    public void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

またはDependencyProperty

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    public bool myProperty
    {
        get { return (bool)GetValue(myPropertyProperty); }
        set { SetValue(myPropertyProperty, value); }
    }

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


    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        myProperty = !myProperty;
    }
}
于 2012-12-24T04:39:51.877 に答える