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"};

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
- 2 回答
- 0 關注
- 299 瀏覽
添加回答
舉報