本文介绍了依赖项属性-文本框HorizontalAlignment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尊敬的先生,
我是WPF的新手,我尝试开发一个简单的面板.在面板中,将文本框和文本块排列为左",右"和居中".
如何从XAML代码获取Textbox HorizontalAlignment为Left或Right,Verticalaignment为顶部或底部
我的代码:
Dear sir,
I am new to WPF, I try to developing a simple panel. In the panel arranging the textbox and textblock to Left, Right and Centre.
How can i get the Textbox HorizontalAlignment is Left or Right and Verticalaignment is top or bottom from the XAML code
my code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
namespace MySimplePanel
{
public class MySimplePanelClass : Panel
{
protected override Size MeasureOverride(Size availableSize)
{
Size maxSize = new Size();
foreach (UIElement child in this.Children)
{
child.Measure(availableSize);
maxSize.Height = Math.Max(child.DesiredSize.Height, maxSize.Height);
maxSize.Width = Math.Max(child.DesiredSize.Width, maxSize.Width);
}
return maxSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
foreach (UIElement child in InternalChildren)
{
child.Arrange(new Rect(finalSize));
//if (HorizontalAlignment == HorizontalAlignment.Right)
//{
//}
}
return finalSize;
}
}
XAML代码:
XAML code :
<window x:class="MySimplePanel.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MySimplePanel"
Title="MainWindow" Height="350" Width="525">
<grid>
<local:mysimplepanelclass grid.columnspan="5" horizontalalignment="Stretch" xmlns:local="#unknown">
<textbox height="23" horizontalalignment="Left" margin="0,10,0,0" name="textBox1" verticalalignment="Top" width="120" />
<textbox height="23" horizontalalignment="Left" margin="100,10,0,0" name="textBox2" verticalalignment="Top" width="120" textalignment="Center" />
<textbox height="23" horizontalalignment="Right" margin="200,10,0,0" name="textBox3" verticalalignment="Top" width="120" />
<textbox height="23" horizontalalignment="Right" margin="360,10,0,0" name="textBox4" verticalalignment="Top" width="120" />
</local:mysimplepanelclass>
</grid>
</window>
感谢所有
Thanks all
推荐答案
protected override Size ArrangeOverride(Size finalSize)
{
foreach (UIElement child in InternalChildren)
{
child.Arrange(new Rect(finalSize));
if (child is TextBox)
{
HorizontalAlignment horizontalAlignment = ((TextBox)child).HorizontalAlignment; //Gets horizontal alignment
VerticalAlignment verticalAlignment = ((TextBox)child).VerticalAlignment; //Gets vertical alignment
}
}
return finalSize;
}
这篇关于依赖项属性-文本框HorizontalAlignment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!