我遇到了同样的问题,不是ToggleButton
,而是TextBox
,我想要格式化用户输入的文本。
在你的情况下,你想改变你的viewmodel中的IsChecked
属性,并立即反映在用户界面中(所以总是被取消选中)。你想要的原因绝对不重要。
问题是,如果您点击ToggleButton
,UWP会调用您的属性的获取方法,就像您期望的那样。 ToggleButton
的正常操作是从未检查变为检查(反之亦然),这就是你的情况。但是,你期望NotifyPropetyChanged
表示UI中的控件。这就是它出错的地方。当setter执行时(包括NotifyPropertyChanged
),getter不会被调用,所以UI并不反映你在setter中做了什么。 这与TwoWay Binding用来做什么(并且仍然在WPF中)完全不同。所以你的代码没有问题,但似乎绑定机制已经改变了,尽管微软声称它没有改变。如果你使用x:Bind
,它可以正常工作,所以帽子可以解决你的问题。
为了更清楚地说明一些事情,我已将您的示例稍作修改,以显示问题。 我已经把页面上的一个ToggleButton
与TwoWay绑定到视图模型,完全一样。点击ToggleButton
会将其状态从已选中状态切换为未选中状态,反之亦然,即使我的viewmodel中的setter始终将该属性设置为false(因此未选中)。 但我还添加了一个普通按钮,我已经绑定到一个命令,该命令还修改了ToggleButton
绑定的属性。点击此按钮将调用ToggleButton
所绑定的属性上的setter。当然,setter被调用的方式是一样的,但在此之后绑定到ToggleButton
被调用,因此在这种情况下NotifyPropertyChanged
确实会导致UI更新。
如果你使用调试器,你可以看到我的意思。 所以你的问题可以通过使用x:Bind来解决,或者通过另一种方法来更新UI,如果Binding仍然像以前一样工作,你不应该这么做。也许微软已经实施了一些现在破坏了经典绑定的优化。
没有特别的事情,只是一个MainPage和一个视图模型。
我对MainPage.xaml中
<Page x:Class="App10.MainPage"
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="using:App10"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<local:ViewModel x:Key="viewModel" />
</Page.Resources>
<Grid x:Name="mainGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Margin="10,20,10,0">
<Button
x:Name="Button"
Content="UWP Normal button"
Command="{Binding Source={StaticResource viewModel}, Path=SwitchIschecked}"
HorizontalAlignment="Stretch" />
<ToggleButton
x:Name="toggleButton"
Margin="0,10,0,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
IsChecked="{Binding Source={StaticResource viewModel}, Path=IsChecked,
Mode=TwoWay}">
<TextBlock>UWP Toggle Button</TextBlock>
</ToggleButton>
</StackPanel>
</Grid>
</Page>
代码为MainPage.xaml.cs中
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App10
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
}
}
而对于ViewModel.cs
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
namespace App10
{
public class ViewModel : INotifyPropertyChanged
{
private bool _isChecked;
// property for TwoWay binding with ToggleButton
public bool IsChecked
{
get
{
return _isChecked;
}
set
{
// extra var just to check 'value'
var _value = value;
// now always set it to false
_isChecked = false;
// Try to pass value of _isChecked to user interface
// because there is no check whether the value really
// has changed
// But this only works if the setter is not being called
// directly from the control the property is bound to
OnPropertyChanged();
}
}
private ICommand _switchChecked;
// ICommand for normal button, binding to Command
// calls method to set Property for ToggleButton
public ICommand SwitchIschecked
{
get
{
if (_switchChecked == null)
_switchChecked = new ChangeChecked(new Action(ChangeVar));
return _switchChecked;
}
set
{
_switchChecked = value;
}
}
// This will set the property for the ToggleButton
private void ChangeVar()
{
IsChecked = !IsChecked;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
/// <summary>
/// Quick class to implement ICommand
/// </summary>
class ChangeChecked : ICommand
{
Action _execute;
public ChangeChecked(Action execute)
{
_execute = execute;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_execute();
}
}
}
来源
2016-07-29 23:57:05
P59
尝试使代码'类模型视图代码'public class – thumbmunkeys
使'class ModelView'成为一个公共类仍然没有正确绑定。:/ ToggleButton仍然会转为“已选中”。 – user3863376
你的'set'正在做'_isEnabled = false;'...... – jsanalytics