「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.03.13
XML
カテゴリ: C#.NET


以下は、C# WPF アプリケーションで `TextBox` の値を `Command` の引数として設定するXAMLのサンプルコードです。


```xml

<Window x:Class="TextBoxCommandParameterSample.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:TextBoxCommandParameterSample"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <TextBox x:Name="textBox" Text="{Binding InputText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="10"/>

        <Button Content="Submit" Command="{Binding SubmitCommand}" CommandParameter="{Binding ElementName=textBox, Path=Text}" Margin="10,0"/>

    </Grid>

</Window>

```


この XAML の例では、`TextBox` に `x:Name` を設定し、バインディングしています。`Button` の `CommandParameter` プロパティには、`Binding` を使って `TextBox` の `Text` プロパティにバインドしています。これにより、`Button` がクリックされたときに `TextBox` の値が `Command` の引数として渡されます。


この XAML を使用すると、`Button` がクリックされると `SubmitCommand` が実行され、`TextBox` の値がそのコマンドの引数として渡されるようになります。






お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.03.13 04:39:24


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
X
Create a Mobile Website
スマートフォン版を閲覧 | PC版を閲覧
Share by: