用到两控件 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;
}
}
}
不会WPF,我觉得这里应该是传chkChecked的值 进去,而不是传这个“chkChecked”字符串进去
不行,试过出错。
因为你这个类没有实现接口:INotifyPropertyChanged
我真的建议你系统地先把wpf基础知识看一遍,或者看书,或者看别人的文章。你现在是通过什么途径学习的?
另外,一般都是新建一个类作为viewmodel,很少有用自身窗体做viewmodel的。
求介绍书籍视频。 我只是看了下刘铁猛《深入浅出WPF》,这个有用吗
上面的代码能否帮我改一下,谢谢
加了这个类,但还是不行?
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;
}
}
}
@tea2007: 这书挺好
@tea2007: 发你邮箱了
@会长: 谢谢