<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<TextBox Name="TextBox1" HorizontalAlignment="Left" PreviewKeyUp="TextBox_PreviewKeyUp"></TextBox>
</StackPanel>
</Grid>
</Window>
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox_PreviewKeyUp(object sender, KeyEventArgs e)
{
TextBox1.Text = "Some text inside Textbox";
MessageBox.Show("TextBox1 Width = " + TextBox1.Width + " & TextBox1.ActualWidth = " + TextBox1.ActualWidth); // NaN & 10
}
}
}
如何动态获取宽度TextBox
?Width
并且ActualWidth
只给我默认的编译时间值,但在编译后我需要 Width 属性。