Thomas의 대답은 정상적으로 작동하지만 추가 종속성 속성이 필요하지 않습니다. OnToggle 메서드를 재정의하고 ViewModel의 IsChecked 바운드 속성을 변경할 수 있도록 ToggleButton에서 상속하는 클래스가 있으면 단추가 올바르게 업데이트됩니다.
XAML :
<myControls:OneWayFromSourceToTargetToggle x:Name="MyCustomToggleButton"
Command="{Binding Path=ToggleDoStuffCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}}"
IsChecked="{Binding Path=ToggleIsCheckedConditionVar,
Mode=OneWay}"
/>
추가 ToggleButton의 등급 : true 또는 false로 뷰 모델 만 설정 부울 ToggleIsCheckedCondition에서 다음
public class OneWayFromSourceToTargetToggle : ToggleButton
{
/// <summary>
/// Overrides the OnToggle method, so it does not set the IsChecked Property automatically
/// </summary>
protected override void OnToggle()
{
// do nothing
}
}
. 좋은 MVVM 관행을 따르고 있기 때문에 좋은 방법입니다.
뷰 모델 :
public bool ToggleIsCheckedCondition
{
get { return _toggleIsCheckedCondition; }
set
{
if (_toggleIsCheckedCondition != value)
{
_toggleIsCheckedCondition = value;
NotifyPropertyChanged("ToggleIsCheckedCondition");
}
}
}
public ICommand ToggleDoStuffCommand
{
get {
return _toggleDoStuffCommand ??
(_toggleDoStuffCommand = new RelayCommand(ExecuteToggleDoStuffCommand));
}
}
private void ExecuteToggleDoStuffCommand(object param)
{
var btn = param as ToggleButton;
if (btn?.IsChecked == null)
{
return;
}
// has not been updated yet at this point
ToggleIsCheckedCondition = btn.IsChecked == false;
// do stuff
}
}
감사합니다. 나는 그것을 시도 할 것이다. –
훌륭한 솔루션. – Ross