탭 페이지에있는 usercontrol의 상황에 맞는 메뉴에서 명령을 바인딩 할 때 문제가 있습니다. 처음 메뉴를 사용하면 (탭을 마우스 오른쪽 버튼으로 클릭) 탭이 잘 작동하지만 탭을 전환하면 명령이 처음 사용 된 databound 인스턴스를 사용하게됩니다.WPF 컨텍스트 메뉴가 데이터 바인딩 된 항목 오른쪽에 바인딩되지 않습니다.
내가 예상대로 작동 UserControl을의 명령에 바인딩 버튼 ...
누군가가 내가 잘못는지 말해 주실 래요을 넣으면?
App.xaml.cs를 :
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
CompanyViewModel model = new CompanyViewModel();
Window1 window = new Window1();
window.DataContext = model;
window.Show();
}
}
Window1.xaml :
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vw="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="HeaderTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type vw:PersonViewModel}">
<vw:UserControl1/>
</DataTemplate>
</Window.Resources>
<Grid>
<TabControl ItemsSource="{Binding Path=Persons}"
ItemTemplate="{StaticResource HeaderTemplate}"
IsSynchronizedWithCurrentItem="True" />
</Grid>
</Window>
UserControl1.xaml :
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinWidth="200">
<UserControl.ContextMenu>
<ContextMenu >
<MenuItem Header="Change" Command="{Binding Path=ChangeCommand}"/>
</ContextMenu>
</UserControl.ContextMenu>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0">The name:</Label>
<TextBox Grid.Column="1" Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</UserControl>
이
문제를 노출하는 테스트 프로젝트입니다Compa nyViewModel.cs :
public class CompanyViewModel
{
public ObservableCollection<PersonViewModel> Persons { get; set; }
public CompanyViewModel()
{
Persons = new ObservableCollection<PersonViewModel>();
Persons.Add(new PersonViewModel(new Person { Name = "Kalle" }));
Persons.Add(new PersonViewModel(new Person { Name = "Nisse" }));
Persons.Add(new PersonViewModel(new Person { Name = "Jocke" }));
}
}
PersonViewModel.cs :
public class PersonViewModel : INotifyPropertyChanged
{
Person _person;
TestCommand _testCommand;
public PersonViewModel(Person person)
{
_person = person;
_testCommand = new TestCommand(this);
}
public ICommand ChangeCommand
{
get
{
return _testCommand;
}
}
public string Name
{
get
{
return _person.Name;
}
set
{
if (value == _person.Name)
return;
_person.Name = value;
OnPropertyChanged("Name");
}
}
void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
TestCommand.cs :
public class TestCommand : ICommand
{
PersonViewModel _person;
public event EventHandler CanExecuteChanged;
public TestCommand(PersonViewModel person)
{
_person = person;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_person.Name = "Changed by command";
}
}
Person.cs :
public class Person
{
public string Name { get; set; }
}
안녕 카메론 :
이
내가이 XAML을 사용하는 경우에만 작동하도록있는 솔루션입니다. 여기에 기술 한 기술이 어떻게 든 내가 여기에서 설명한 문제와 관련 있다고 생각합니까? http://stackoverflow.com/questions/833607/wpf-why-do-contextmenu-items-work-for-listbox-but-not- itemscontrol ... 나는 명령에 구속력이 없지만 관련 문제인 것은 의심 스럽다. –나는이 대답으로 확신하지 못한다. 명령 바인딩은 메뉴 항목에 대해 작동합니다 (보기 모델을 바인딩해야 함을 알고 있음) ... 문제는 탭 전환으로 인해 datacontext가 변경 될 때 메뉴 항목이 다시 바인딩되지 않는다는 것입니다. 비주얼 트리의 일부가 아니기 때문에 처음으로 어떻게 작동합니까? – Schneider
@Schneider : 메뉴의 바인딩이 작동하지 않는다고 말하지 않았습니다. 부모님이 기대하는 것처럼 데이터 인터페이스를 상속하지 않는다고 말하지 않았습니다. WPF 바인딩 엔진은 메뉴가 처음 열릴 때 컨텍스트를 설정하고 탭이 변경되면 업데이트하지 않는다고 말하고 싶습니다. –