Built-in Commands (Copy, Paste, Cut) ব্যবহার গাইড ও নোট

Microsoft Technologies - ডব্লিউপিএফ (WPF) - Command Binding এবং Routed Commands
383

WPF (Windows Presentation Foundation) অ্যাপ্লিকেশনে Copy, Paste, এবং Cut এই তিনটি কমান্ড সাধারণত ইউজার ইন্টারফেসে ডেটা ম্যানিপুলেশনের জন্য ব্যবহৃত হয়। WPF তে এই কমান্ডগুলোর জন্য ইন-বিল্ট সমর্থন রয়েছে, যা আপনাকে দ্রুত এবং সহজভাবে ক্লিপবোর্ডের মাধ্যমে ডেটা কপি, কাট এবং পেস্ট করার সুযোগ দেয়।

Copy, Paste, এবং Cut কমান্ডের মৌলিক ধারণা

  • Copy: নির্বাচিত ডেটাকে ক্লিপবোর্ডে কপি করে, যাতে এটি পরে পেস্ট করা যায়।
  • Cut: নির্বাচিত ডেটাকে ক্লিপবোর্ডে কাট করে, এবং মূল জায়গা থেকে ডেটাটি মুছে দেয়।
  • Paste: ক্লিপবোর্ড থেকে কপি করা বা কাট করা ডেটাকে বর্তমান অবস্থানে পেস্ট করে।

WPF অ্যাপ্লিকেশনগুলিতে এই কমান্ডগুলো ব্যবহারের জন্য ICommand ইন্টারফেস এবং Clipboard ক্লাস ব্যবহৃত হয়।


WPF এ Built-in Commands ব্যবহার করা

WPF তে Copy, Paste, এবং Cut কমান্ডগুলোর জন্য আপনি CommandBindings ব্যবহার করতে পারেন, যা UI কন্ট্রোলের সাথে যুক্ত করে, যাতে একটি নির্দিষ্ট কমান্ড নির্দিষ্ট UI উপাদান (যেমন বাটন, টেক্সটবক্স ইত্যাদি) থেকে ট্রিগার করা যায়।

1. CommandBindings এবং Built-in Commands

WPF এ Built-in Commands হল ApplicationCommands.Copy, ApplicationCommands.Cut, এবং ApplicationCommands.Paste। এগুলো অ্যাপ্লিকেশনের স্ট্যান্ডার্ড কমান্ড হিসাবে ব্যবহৃত হয় এবং CommandBindings এর মাধ্যমে কন্ট্রোলের সাথে যুক্ত করা হয়।

Copy, Cut, Paste কমান্ডের জন্য XAML উদাহরণ

<Window x:Class="WPFApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Built-in Commands Example" Height="350" Width="525">
    <Window.CommandBindings>
        <!-- CommandBinding for Cut -->
        <CommandBinding Command="Cut" Executed="Cut_Executed" CanExecute="Cut_CanExecute"/>
        <!-- CommandBinding for Copy -->
        <CommandBinding Command="Copy" Executed="Copy_Executed" CanExecute="Copy_CanExecute"/>
        <!-- CommandBinding for Paste -->
        <CommandBinding Command="Paste" Executed="Paste_Executed" CanExecute="Paste_CanExecute"/>
    </Window.CommandBindings>

    <Grid>
        <TextBox Name="myTextBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="300" Height="100" Margin="10"/>
        <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="10">
            <Button Content="Cut" Command="Cut" Width="75"/>
            <Button Content="Copy" Command="Copy" Width="75"/>
            <Button Content="Paste" Command="Paste" Width="75"/>
        </StackPanel>
    </Grid>
</Window>

এখানে:

  • CommandBindings ব্যবহার করে আমরা Cut, Copy, এবং Paste কমান্ডগুলোর জন্য কাস্টম এক্সিকিউশন এবং CanExecute হ্যান্ডলার যোগ করেছি।
  • TextBox কন্ট্রোলের মাধ্যমে ইউজার টেক্সট এন্টার করতে পারবেন এবং বাটনগুলোর মাধ্যমে কপি, কাট এবং পেস্ট করতে পারবেন।

C# কোডে কমান্ড হ্যান্ডলিং

using System.Windows;
using System.Windows.Input;

namespace WPFApplication
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // CanExecute for Cut command
        private void Cut_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = myTextBox.SelectionLength > 0; // Cut is allowed only if text is selected
        }

        // Executed for Cut command
        private void Cut_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            myTextBox.Cut(); // Perform the cut operation
        }

        // CanExecute for Copy command
        private void Copy_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = myTextBox.SelectionLength > 0; // Copy is allowed only if text is selected
        }

        // Executed for Copy command
        private void Copy_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            myTextBox.Copy(); // Perform the copy operation
        }

        // CanExecute for Paste command
        private void Paste_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = Clipboard.ContainsText(); // Paste is allowed only if clipboard contains text
        }

        // Executed for Paste command
        private void Paste_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            myTextBox.Paste(); // Perform the paste operation
        }
    }
}

এখানে:

  • CanExecute: এই মেথডটি চেক করে যে নির্দিষ্ট কমান্ডটি কার্যকর হবে কিনা। উদাহরণস্বরূপ, Cut কমান্ডটি তখনই কার্যকর হবে যদি টেক্সট বক্সে কোন নির্বাচিত টেক্সট থাকে।
  • Executed: এই মেথডটি কমান্ডটি কার্যকর হওয়ার সময় চালিত হয়। এটি কপি, কাট বা পেস্ট অপারেশন বাস্তবায়িত করে।

Built-in Commands এর সুবিধা

  1. সহজ ইন্টিগ্রেশন: WPF এ ApplicationCommands.Copy, ApplicationCommands.Cut, এবং ApplicationCommands.Paste কমান্ডগুলি অন্তর্নির্মিত থাকে, তাই এগুলি কাস্টমাইজ বা এক্সটেন্ড করা সহজ। আপনি শুধুমাত্র CommandBindings ব্যবহার করে এই কমান্ডগুলি UI কন্ট্রোলের সাথে যুক্ত করতে পারেন।
  2. UI এর মাধ্যমে Command সমর্থন: Command প্রপার্টি ব্যবহার করে আপনি কমান্ডগুলিকে UI কন্ট্রোল (যেমন বাটন, মেনু আইটেম) এর সাথে একত্রে ব্যবহার করতে পারেন।
  3. ডেটা ম্যানিপুলেশন: কপি, কাট এবং পেস্ট অপারেশন ক্লিপবোর্ডের মাধ্যমে ডেটা ম্যানিপুলেশন করতে সহায়ক, যা আপনার অ্যাপ্লিকেশনে ডেটা প্রবাহ সহজ করে দেয়।

সারাংশ (Summary)

  • Built-in Commands: WPF তে Copy, Cut, এবং Paste কমান্ডগুলো ইন-বিল্ট থাকে এবং CommandBindings ব্যবহার করে UI কন্ট্রোলের সাথে যুক্ত করা যায়।
  • CanExecute: কমান্ডটি কার্যকর হবে কিনা তা নির্ধারণ করে।
  • Executed: কমান্ডটির কার্যাবলী বাস্তবায়ন করে।

এভাবে, WPF তে Copy, Paste, এবং Cut কমান্ডগুলো সহজে প্রয়োগ করা যায় এবং ব্যবহারকারীদের জন্য একটি ফ্লুইড এবং ইন্টারঅ্যাকটিভ ডেটা ম্যানিপুলেশন সিস্টেম তৈরি করা সম্ভব।

Content added By
Promotion

Are you sure to start over?

Loading...