首页 新闻 会员 周边

c# wpf几行简单代码,求找出问题,谢谢!

0
悬赏园豆:10 [已解决问题] 解决于 2023-01-10 09:14

用到两控件 checkbox与button 实现功能:点击button checkbox打勾,再点击button checkbox取消打勾.这样循环,点一下按钮变一次。遇到问题:点击按钮后,checkbox没反应,不会打勾。
代码如下:
MainWindow.xaml

<Window
x:Class="Wpfbinding3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Wpfbinding3"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel Orientation="Vertical">
<CheckBox Name="checkbox" Content="java" IsChecked="{Binding ChkChecked}" />
<Button Click="Button_Click" Content="click" RenderTransformOrigin="0.497,5.645" />
</StackPanel>
</Grid>
</Window>

//****************************************************************************************************
MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
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 Wpfbinding3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}

   // public bool ChkChecked { get; set; }

    private bool chkChecked = false;
    public bool ChkChecked
    {
        get
        {
            return chkChecked;
        }
        set
        {
            chkChecked = value;
            RaisePropertyChanged("ChkChecked");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string _Prop)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(_Prop));
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ChkChecked = !ChkChecked;
    }
}

}

tea2007的主页 tea2007 | 初学一级 | 园豆:1
提问于:2023-01-07 10:08
< >
分享
最佳答案
0


不会WPF,我觉得这里应该是传chkChecked的值 进去,而不是传这个“chkChecked”字符串进去

收获园豆:10
中华鲟3670 | 小虾三级 |园豆:847 | 2023-01-07 11:11

不行,试过出错。

tea2007 | 园豆:1 (初学一级) | 2023-01-07 11:15
其他回答(1)
0

因为你这个类没有实现接口:INotifyPropertyChanged

我真的建议你系统地先把wpf基础知识看一遍,或者看书,或者看别人的文章。你现在是通过什么途径学习的?

另外,一般都是新建一个类作为viewmodel,很少有用自身窗体做viewmodel的。

会长 | 园豆:12401 (专家六级) | 2023-01-07 11:25

求介绍书籍视频。 我只是看了下刘铁猛《深入浅出WPF》,这个有用吗

支持(0) 反对(0) tea2007 | 园豆:1 (初学一级) | 2023-01-07 11:35

上面的代码能否帮我改一下,谢谢

支持(0) 反对(0) tea2007 | 园豆:1 (初学一级) | 2023-01-07 11:38

加了这个类,但还是不行?
Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace Wpfbinding3
{
public class Person : INotifyPropertyChanged
{
private bool chkChecked = false;
public bool ChkChecked
{
get
{
return chkChecked;
}
set
{
chkChecked = value;
RaisePropertyChanged("ChkChecked");
}
}

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string _Prop)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(_Prop));
        }
    }
}

}

//****************************************************************************************************
MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
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 Wpfbinding3
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
private Person p1 = new Person();

private void Button_Click(object sender, RoutedEventArgs e)
{
p1.ChkChecked = !p1.ChkChecked;
}
}
}

支持(0) 反对(0) tea2007 | 园豆:1 (初学一级) | 2023-01-07 14:25

@tea2007: 这书挺好

支持(0) 反对(0) 会长 | 园豆:12401 (专家六级) | 2023-01-08 19:55

@tea2007: 发你邮箱了

支持(0) 反对(0) 会长 | 园豆:12401 (专家六级) | 2023-01-09 13:56

@会长: 谢谢

支持(0) 反对(0) tea2007 | 园豆:1 (初学一级) | 2023-01-09 14:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册