繁体   English   中英

使用INotifyPropertyChanged从代码后面的C#Wpf设置切换按钮

[英]C# Wpf setting toggle button from behind code using INotifyPropertyChanged

我有一个简单的切换按钮,效果很好。 我可以单击切换按钮并更改它显示的图像。 我现在想做的事情与后面的代码相同。 找到类似的链接

编辑:这是我想做的

我阅读了以下线程,该线程确切地说明了我需要做什么
WPF ToggleButton.IsChecked绑定不起作用

以编程方式,我的代码似乎没有任何作用。 如果我单击UI可以正常工作,但是我真的想从程序中更改状态。 下面的程序只是一个原型。

我无法弄清楚我的XAML或代码有什么问题。 最后决定将所有内容粘贴为测试程序!

Xaml:

<Window x:Class="ToggleButtonImageChange.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ToggleButtonImageChange"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

        <Image Source="secured.jpg"

         x:Key="MyImage1" />

        <Image Source="unsecured.jpg"

         x:Key="MyImage2" />



        <Style TargetType="{x:Type ToggleButton}"

         x:Key="MyToggleButtonStyle">

            <Setter Property="Content"

            Value="{DynamicResource MyImage2}" />

            <Style.Triggers>

                <Trigger Property="IsChecked"

               Value="True">

                    <Setter Property="Content"

                Value="{DynamicResource MyImage2}" />

                </Trigger>

            </Style.Triggers>

        </Style>

    </Window.Resources>

    <Grid>

        <ToggleButton Style="{StaticResource MyToggleButtonStyle}" Name="tgbtn" Margin="0,29,0,139" IsChecked="{Binding Path=isAdmin, Mode=TwoWay}"/>

    </Grid>
</Window>

后面的代码:

namespace ToggleButtonImageChange
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        bool _isAdmin;
        public MainWindow()
        {
            InitializeComponent();

             isAdmin = true;
            OnPropertyChanged("isAdmin");

        }
         public bool isAdmin
        {
            get
            {
                return _isAdmin;
            }

            set
            {
                _isAdmin = value;
                OnPropertyChanged("isAdmin");
            }
        }

        private void OnPropertyChanged(string p)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }

我进入调试器,发现即使将isAdmin设置为true,按钮isChecked仍为false,因此显示了不正确的图像。 我不太明白做错了什么以及如何通过代码更改isChecked。

尝试将xaml文件更改为此:

<Window x:Class="WpfApplication1.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"
        x:Name="TestWindow">
    <Window.Resources>
        <Image Source="secured.png" x:Key="MyImage1" />
        <Image Source="unsecured.png" x:Key="MyImage2" />
        <Style TargetType="{x:Type ToggleButton}" x:Key="MyToggleButtonStyle">
            <Setter Property="Content" Value="{DynamicResource MyImage2}" />
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Content" Value="{DynamicResource MyImage1}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <ToggleButton x:Name="tgbtn"
                      Margin="0,29,0,139" 
                      Style="{StaticResource MyToggleButtonStyle}"
                      IsChecked="{Binding Path=isAdmin, Mode=TwoWay, ElementName=TestWindow}"/>
    </Grid>
</Window>

注意,默认的Content值使用MyImage2 ,但是触发器将其设置为MyImage1-它们只需要是不同的图像即可。

还要注意我添加到根窗口元素的x:Name =“ TestWindow” -稍后在绑定中使用:

{Binding Path=isAdmin, Mode=TwoWay, ElementName=TestWindow}

我相信,基本上这就是进行更改以使其按预期运行所需的全部内容。

您也可以像下面这样在代码中留下构造函数,但这是可选的更改:

public MainWindow()
{
    InitializeComponent();
    isAdmin = true;
}

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM