में कमांड लक्ष्य सेट करना मुझे रूटेड कॉमांड के लिए कमांडटाइटल प्रॉपर्टी को समझने में कठिनाई हो रही है।XAML
असल में, मेरे पास कुछ स्थिर आदेश हैं जिनके पास उपयोगकर्ता नियंत्रण में कार्यान्वयन है (खिड़की नहीं)। मैं उपयोगकर्ता नियंत्रण में एक कमांडबाइंडिंग बनाते हैं। अगर मैं usercontrol में बटन घोषित करता हूं, तो मैं अपने रूटेड ईवेंट का उपयोग करने में सक्षम हूं। हालांकि, जब बटन उपयोगकर्ता नियंत्रण से बाहर होता है, तो मैं अपने रूटेड ईवेंट का उपयोग नहीं कर सकता। मुझे लगता है कि कमांड लक्ष्य मेरी समस्या का समाधान करेगा।
तो मैं टूलबार उपयोगकर्ता नियंत्रण के बटन के लिए कमांडटाइटल कैसे सेट करूं, ताकि कंटेनर के निष्पादित और CanExecuted को कॉल किया जा सके?
माइयाहटन परिवर्तनों में परिवर्तन के साथ संपादित कोड, लेकिन मैं अभी भी इसे CanExecute या निष्पादित नहीं कर सकता।
विंडो XAML:
<Window x:Class="RoutedCommands.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RoutedCommands"
xmlns:toolbar="clr-namespace:RoutedCommands.Toolbar"
Title="Window1" Height="300" Width="300">
<StackPanel>
<local:Container Width="100" Height="25" x:Name="MyContainer" />
<toolbar:Toolbar Width="100" Height="25" CommandTarget="{Binding MyContainer}" />
</StackPanel>
</Window>
टूलबार XAML:
<UserControl x:Class="RoutedCommands.Toolbar.Toolbar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RoutedCommands"
x:Name="MyToolbar"
Height="300" Width="300">
<Grid>
<Button Command="{x:Static local:Commands.MyCommand}" Content="Try Me" CommandTarget="{Binding ElementName=MyToolbar, Path=CommandTarget, Mode=OneWay}" />
</Grid>
</UserControl>
टूलबार सीएस:
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
// Using a DependencyProperty as the backing store for CommandTarget. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandTargetProperty =
DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(Toolbar), new UIPropertyMetadata(null));
public IInputElement CommandTarget
{
get { return (IInputElement)GetValue(CommandTargetProperty); }
set { SetValue(CommandTargetProperty, value); }
}
}
कंटेनर XAML:
<UserControl x:Class="RoutedCommands.Container"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RoutedCommands"
Height="300" Width="300">
<UserControl.CommandBindings>
<CommandBinding Command="{x:Static local:Commands.MyCommand}" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" />
</UserControl.CommandBindings>
<Grid>
<Button Command="{x:Static local:Commands.MyCommand}" Content="Click Me" />
</Grid>
</UserControl>
कंटेनर सीएस:
public partial class Container : UserControl
{
public Container()
{
InitializeComponent();
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
Console.WriteLine("My Command Executed");
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
Console.WriteLine("My Command Can Execute");
e.CanExecute = true;
}
}
RoutedCommands:
namespace RoutedCommands
{
public static class Commands
{
public static readonly RoutedUICommand MyCommand = new RoutedUICommand();
}
}
मैं अभी तक अपने प्रश्न का उत्तर प्राप्त करने में सक्षम नहीं किया गया है, लेकिन मैं कर रहा हूँ मेरे पास अब तक जो काम है, उसके साथ काम करने की कोशिश कर रहा हूं। – kevindaub
कुछ कोड पोस्ट किया गया है जो आपके परिदृश्य को काम पर ले जाना चाहिए। – micahtan
यह वास्तव में मदद नहीं की थी। मैं आज रात कुछ और कोशिश करूँगा। – kevindaub