0

だから私はボタンを持っています。クラスの整数プロパティの値に応じて、ボタンの可視性を設定したいと考えています。これには、データ バインディングとコンバーターが必要です。

ボタンの XAML コードは次のとおりです。

<Window.Resources>
        <local:Button1VisibilityConverter x:Key="Button1VisibilityConverter"/>
        <local:ModeValues x:Key="ModeHolder"/>
    </Window.Resources>
    <Grid>
        <StackPanel HorizontalAlignment="Left" Height="150" Margin="92,90,0,0" VerticalAlignment="Top" Width="301">
            <Button Content="1" Height="58" Background="#FFA20000" Foreground="White" Visibility="{Binding Source={StaticResource ModeHolder}, Path=State, Converter=Button1VisibilityConverter}"/>
            <Button Content="2" Height="58" Background="#FF16A200" Foreground="White"/>
            <Button Content="3" Height="58" Background="#FF4200A2" Foreground="White"/>
        </StackPanel>
    </Grid>

私のコンバーターは次のとおりです。

class Button1VisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targettype, object parameter, System.Globalization.CultureInfo culture)
        {
            int mode = (int)value;
            if (mode == ModeValues.Red)
                return System.Windows.Visibility.Visible;
            else
                return System.Windows.Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

可視性を制御したいプロパティを持つクラスは次のとおりです。

public class ModeValues : IObservable<int>
    {
        private int _state = -1;

        public static int Red
        {
            get
            {
                return 0;
            }
        }

        public static int Green
        {
            get
            {
                return 1;
            }
        }

        public static int Purple
        {
            get
            {
                return 2;
            }
        }

        public int State
        {
            get
            {
                return this._state;
            }
            set
            {
                this.State = value;
            }
        }
    }

なぜ機能しないのかわかりません。ModeHolder のインスタンスのプロパティに可視性をバインドし、ModeHolder を監視可能にし、int を可視性に変換する必要があると考えました。私は何が欠けていますか?

4

1 に答える 1