【问题标题】:C# Wpf setting toggle button from behind code using INotifyPropertyChangedC# Wpf 使用 INotifyPropertyChanged 从代码后面设置切换按钮
【发布时间】:2013-05-31 05:27:14
【问题描述】:

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

编辑:这就是我想要做的

我阅读了以下线程,该线程确切地告诉了我需要做什么
WPF ToggleButton.IsChecked binding does not work

以编程方式,我的代码似乎没有任何效果。如果我单击 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。

【问题讨论】:

  • 您如何使用问题顶部显示的代码调用该函数?
  • 您的代码非常适合我!我有另一个按钮,在 clickEvent 中我有 tgbtn.IsChecked =!tgbtn.IsChecked; .这对我来说很好。
  • @KristianK。我只是按照我展示它的方式使用它。如果(管理员){ tgbtn.IsChecked = true;} 。默认情况下,该按钮未选中。
  • @Manoj 我没有单独的按钮。我只有一个名为 tgbtn 的切换按钮。默认情况下,该按钮未选中。我想在代码中执行以下操作: if(admin) { tgbtn.IsChecked = true;} 。当我这样做时,我希望我的按钮图像会改变。你能做到吗?
  • 窗口已经加载了吗?也许您需要更新 Loaded 事件的状态?

标签: c# wpf toggle


【解决方案1】:

尝试将 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;
}

希望对您有所帮助。

【讨论】:

  • 我仍然无法让它工作 :-( 默认图像是 MyImage2(unsecured.jpg) 。但是在代码中,我将 isAdmin 设置为 true,这应该将 IsChecked 设置为 true,因此 MyImage1(安全。 jpg)应该在启动时看到。但是当我开始时,我继续得到不安全的图像。如果它对你有用,你也可以发布 .cs。也许我错过了那里的东西。
  • 好的,我再检查一遍。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 2015-03-18
  • 1970-01-01
相关资源
最近更新 更多