C# WPF コントロールのテキストを設定する
WPFで、コントロールのテキストを設定する手順を記述してます。
環境
- OS windows10 pro 64bit
- Microsoft Visual Studio Community 2022 Version 17.2.392
コントロールのテキストを設定する
コントロールのテキストを設定するには、XAML内にある「Content」を追加および編集します。
<Window
x:Class="sample.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:local="clr-namespace:sample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<Label
Width="100"
Height="30"
Content="labelです" />
</Grid>
</Window>
プロパティウィンドウの「Content」からも編集可能です。
nameを指定
nameを指定して、コードビハインド側で編集することもできます。
<Window
x:Class="sample.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:local="clr-namespace:sample"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<Label
x:Name="Lalel1"
Width="100"
Height="30"
Content="labelだよ" />
</Grid>
</Window>
「MainWindow.xaml.cs」で「Content」を設定します。
namespace sample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Lalel1.Content = "labalやで";
}
}
}
実行結果
-
前の記事
Oracle Database 範囲を指定して値を抽出する 2022.09.17
-
次の記事
python 空文字判定処理で「==」と「not」と「is」のパフォーマンスを計測して比較する 2022.09.18
コメントを書く