मैं समझने की कोशिश कर रहा हूं कि यूआई से हटाए गए कमांड स्रोत पर CanExecute क्यों लागू किया गया है। यहाँ यह दर्शाने के एक सरल कार्यक्रम है:यूआई से कमांड स्रोत हटा दिए जाने के बाद CanExecute क्यों लागू किया जाता है?
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="350" Width="525">
<StackPanel>
<ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Content="{Binding Txt}"
Command="{Binding Act}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Remove first item" Click="Button_Click" />
</StackPanel>
</Window>
कोड-पीछे:
Removed item marked 'Removed'
Item removed
Why is this happening?
Why is this happening?
:
public partial class MainWindow : Window
{
public class Foo
{
static int _seq = 0;
int _txt = _seq++;
RelayCommand _act;
public bool Removed = false;
public string Txt { get { return _txt.ToString(); } }
public ICommand Act
{
get
{
if (_act == null) {
_act = new RelayCommand(
param => { },
param => {
if (Removed)
Console.WriteLine("Why is this happening?");
return true;
});
}
return _act;
}
}
}
public ObservableCollection<Foo> Items { get; set; }
public MainWindow()
{
Items = new ObservableCollection<Foo>();
Items.Add(new Foo());
Items.Add(new Foo());
Items.CollectionChanged +=
new NotifyCollectionChangedEventHandler(Items_CollectionChanged);
DataContext = this;
InitializeComponent();
}
void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Remove)
foreach (Foo foo in e.OldItems) {
foo.Removed = true;
Console.WriteLine("Removed item marked 'Removed'");
}
}
void Button_Click(object sender, RoutedEventArgs e)
{
Items.RemoveAt(0);
Console.WriteLine("Item removed");
}
}
जब मैं बटन एक बार "पहला आइटम निकालें" पर क्लिक करें, मैं इस आउटपुट प्राप्त
"यह क्यों हो रहा है?" प्रत्येक बार जब मैं खिड़की के कुछ खाली हिस्से पर क्लिक करता हूं तो मुद्रित रहता रहता है।
ऐसा क्यों हो रहा है? और हटाए गए कमांड स्रोतों पर CanExecute को लागू होने से रोकने के लिए मुझे क्या करना चाहिए या क्या करना चाहिए?
नोट: रिलेकॉमैंड here पाया जा सकता है।
माइकल Edenfield प्रश्नों के उत्तर:
Q1: जब CanExecute हटा बटन पर शुरू हो जाती है की Callstack:
WpfApplication1.exe WpfApplication1.MainWindow.Foo.get_Act.AnonymousMethod__1 (ऑब्जेक्ट पैरा) लाइन 30 WpfApplication1.exe! WpfApplication1.RelayCommand.CanExecute (ऑब्जेक्ट पैरामीटर) लाइन 41 + 0x1a बाइट्स प्रस्तुति Framework.dll! MS.Internal.Commands.CommandHelpers.CanExecuteCommandSource (Syste m.Windows.Input.ICommandSource कमांडसोर्स) + 0x8a बाइट्स प्रेजेंटेशनफ्रेमवर्क.dll! System.Windows.Controls.Primitives.ButtonBase.UpdateCanExecute() + 0x18 बाइट्स प्रस्तुति Framework.dll! System.Windows.Controls.Primitives.ButtonBase.OnCanExecuteChanged (ऑब्जेक्ट प्रेषक, System.EventArgs ई) + 0x5 बाइट प्रस्तुति Core.dll! System.Windows.Input.CommandManager.CallWeakReferenceHandlers (System.Collections.Generic.List हैंडलर) + 0xac बाइट प्रस्तुति Core.dll! System.Windows.Input.CommandManager। RaiseRequerySuggested (वस्तु obj) + 0xf बाइट्स
Q2: (? सिर्फ पहला नहीं) इसके अलावा, यह हो रहा रखता है, तो आप सूची से बटन के सभी हटाने
हां।
मुझे रिलेकॉमैंड याद आती है। यह क्या है? – Gqqnbig
मैंने रिलेकॉमैंड के कार्यान्वयन के लिए एक लिंक जोड़ा। –
क्या आपने ईवेंट के दौरान कॉलस्टैक की जांच करने की कोशिश की है और यह देखने के लिए कि क्या ट्रिगर हुआ? साथ ही, क्या यह तब भी होता है जब आप सूची के सभी * बटनों को हटाते हैं (केवल पहले नहीं?) –