मुझे एक पृष्ठ पर मौजूद उपयोगकर्ता नियंत्रण पर संदर्भ मेनू में कमांड को बाध्य करते समय समस्या है। पहली बार जब मैं मेनू का उपयोग करता हूं (टैब पर राइट-क्लिक करें) यह बहुत अच्छा काम करता है, लेकिन यदि मैं टैब स्विच करता हूं तो कमांड डाटाबेस इंस्टेंस का उपयोग करेगा जो पहली बार उपयोग किया गया था।डब्ल्यूपीएफ संदर्भ मेनू सही डेटाबेस आइटम से बंधे नहीं है
अगर मैं एक बटन है कि 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>
यह एक परीक्षण परियोजना है कि समस्या को उजागर करता है
कॉम्पा 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 ... मैं एक आदेश के लिए बाध्यकारी नहीं हूँ, लेकिन मुझे संदेह है कि यह एक संबंधित समस्या है। –
मुझे इस जवाब से आश्वस्त नहीं है। कमांड बाइंडिंग मेनू आइटम के लिए काम करते हैं (यह जानता है कि इसे व्यू मॉडल को बांधना है) ... समस्या यह है कि मेन्यूटेम स्विच नहीं करते हैं जब स्विचिंग टैब के कारण डेटाकॉन्टेक्स्ट बदल जाता है। यदि उनके कारण दृश्य वृक्ष का हिस्सा नहीं है, तो यह पहली बार कैसे काम करता है? – Schneider
@ स्क्नीडर: मैंने यह नहीं कहा कि मेनू में बाइंडिंग काम नहीं करती है, सिर्फ इसलिए कि वे अपने माता-पिता से अपने डेटाैकेंटेक्स्ट का वारिस नहीं करते हैं जैसे आप उम्मीद करेंगे। मैं कहूंगा कि डब्ल्यूपीएफ बाध्यकारी इंजन संदर्भ को सेट कर रहा है जब मेनू पहले खोला गया है और फिर टैब बदलते समय इसे अपडेट नहीं कर रहा है। –