WPF 응용 프로그램에서 단추와 함께 Command 및 CommandParameter 바인딩을 사용하려고합니다. 나는 정확히 똑같은 코드가 Silverlight에서 잘 작동하므로 내가 잘못한 것을 궁금해하고있다. 내 Silverlight 응용 프로그램으로WPF CommandParameter 바인딩이 업데이트되지 않습니다.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
combo.ItemsSource = new List<string>(){
"One", "Two", "Three", "Four", "Five"
};
this.DataContext = this;
}
public TestCommand TestCommand
{
get
{
return new TestCommand();
}
}
}
public class TestCommand : ICommand
{
public bool CanExecute(object parameter)
{
return parameter is string && (string)parameter != "Two";
}
public void Execute(object parameter)
{
MessageBox.Show(parameter as string);
}
public event EventHandler CanExecuteChanged;
}
:
이<Window x:Class="WPFCommandBindingProblem.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">
<StackPanel Orientation="Horizontal">
<ComboBox x:Name="combo" VerticalAlignment="Top" />
<Button Content="Do Something" Command="{Binding Path=TestCommand}"
CommandParameter="{Binding Path=SelectedItem, ElementName=combo}"
VerticalAlignment="Top"/>
</StackPanel>
</Window>
코드 뒤에 다음과 같다 :
나는 명령 매개 변수가 콤보 상자의 selectedItem에 바인딩 된 콤보 상자와 버튼을 가지고 콤보 상자의 SelectedItem이 변경되면 CommandParameter 바인딩은 현재 선택된 항목으로 내 명령의 CanExecute 메서드를 다시 계산하고 이에 따라 단추 사용 가능 상태가 업데이트됩니다.
WPF를 사용하면 XAML이 구문 분석 될 때 바인딩이 만들어 질 때만 CanExecute 메서드가 호출됩니다.
아이디어가 있으십니까?
변경되는 UI의 모든 속성에 대한 모든 명령을 다시 평가합니까? Delegate 명령과 함께 사용할 방법이 없습니까? Silverlight에서 이와 같은 간단한 또는 ootb 솔루션이 있습니까? – Firo
이것은 가장 단순한 방법입니다 - CanExecuteChanged 이벤트를 호출 할시기를 제어합니다 - 프레임 워크가 업데이트를 결정할 때마다 업데이트하도록 설정합니다. – Goblin