亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何設置 WPF 按鈕的文本

如何設置 WPF 按鈕的文本

C#
陪伴而非守候 2022-12-31 10:31:46
我不知道如何在按鈕上顯示文本?,F在這是我問題的根源。我有這三個按鈕,我想顯示一個小文本:“開始”、“停止”和“重置”。using System.Windows.Controls;namespace Program.src.View.Main{    public class LowerActionPanel : StackPanel    {        public LowerActionPanel()        {            this.Orientation = Orientation.Horizontal;            this.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;            Button startButton = new Button();            startButton.Width = 90;            startButton.Height = 25;            startButton.Text = "Text"; //(here the problem lies)            this.Children.Add(startButton);            Button stopButton = new Button();            stopButton.Width = 90;            stopButton.Height = 25;            this.Children.Add(stopButton);            Button resetButton = new Button();            resetButton.Width = 90;            resetButton.Height = 25;            this.Children.Add(resetButton);        }    }}在另一個問題中,他們使用 .Text 沒有任何問題,這讓我認為可以只使用它,或者我只是弄錯了?
查看完整描述

2 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

您正在使用 WPF。您提供的最后一個示例鏈接使用 WinForms。WinForms 在按鈕上提供屬性 Text 而 WPF 按鈕沒有。如果要設置 WPF 按鈕的內容,應使用 Content 屬性。


像這樣:


var button = new Button();

button.Content = "Click here";

或者使用對象初始值設定項:


var button = new Button {Content = "Click here"};


查看完整回答
反對 回復 2022-12-31
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

我認為您正在做的可能應該是用戶控件而不是自定義控件。


也許是這樣的:


<UserControl x:Class="wpf_99.UserControl1"

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

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

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

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

         xmlns:local="clr-namespace:wpf_99"

         mc:Ignorable="d" 

         Height="25"

         >

<UserControl.Resources>

    <Style TargetType="Button">

        <Setter Property="Width" Value="92"/>

        <Setter Property="Margin" Value="2"/>

    </Style>

</UserControl.Resources>

<StackPanel Orientation="Horizontal">

    <Button Name="StartButton" Content="Start"/>

    <Button Name="StopButton" Content="Stop"/>

    <Button Name="ResetButton" Content="Reset"/>

</StackPanel>

</UserControl>

用法:


 <Window

    ...

    xmlns:local="clr-namespace:wpf_99"

    >


<Grid>

    <local:UserControl1 HorizontalAlignment="Left" VerticalAlignment="Top"/>

</Grid>

您可能會發現這很有用:


https://social.technet.microsoft.com/wiki/contents/articles/32610.wpf-layout-lab.aspx


查看完整回答
反對 回復 2022-12-31
  • 2 回答
  • 0 關注
  • 299 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號