依赖项属性-文本框Horizo​​ntalAlignment [英] Dependency Property - Textbox HorizontalAlignment

查看:47
本文介绍了依赖项属性-文本框Horizo​​ntalAlignment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生,

我是WPF的新手,我尝试开发一个简单的面板.在面板中,将文本框和文本块排列为左",右"和居中".

如何从XAML代码获取Textbox Horizo​​ntalAlignment为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

推荐答案

在您的代码中尝试以下操作-
Try the following in your code -
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;
}


可以更改TextBox的宽度或边距或厚度
谢谢

代码:
受保护的重写Size ArrangeOverride(Size finalSize)
{
双倍x = 0.0;
y = 0.0;
双倍fw = finalSize.Width;
double TotRightWidth = 0.0;
double TotWidth = 0.0;

Horizo​​ntalAlignment horizo​​ntalAlignment = Horizo​​ntalAlignment;

foreach(this.Children中的UIElement子级)
{
如果(子项是TextBox)
{
horizo​​ntalAlignment =((TextBox)child).Horizo​​ntalAlignment; //获取水平对齐方式
TotWidth + = child.DesiredSize.Width;
}
如果(子级为TextBlock)
{
horizo​​ntalAlignment =(((TextBlock)child)子元素. //获取水平对齐方式
TotWidth + = child.DesiredSize.Width;
}
如果(horizo​​ntalAlignment == Horizo​​ntalAlignment.Right)
{
TotRightWidth + = child.DesiredSize.Width;
}
}

double DiffWidth =(TotWidth-finalSize.Width)/Children.Count;
fw = finalSize.Width-TotRightWidth;

foreach(儿童中的UIElement儿童)
{
如果(子项是TextBox)
{
horizo​​ntalAlignment =((TextBox)child).Horizo​​ntalAlignment; //获取水平对齐方式
//VerticalAlignment verticalAlignment =(((TextBox)child).VerticalAlignment; //获取垂直对齐方式
}
如果(子级为TextBlock)
{
horizo​​ntalAlignment =(((TextBlock)child)子元素. //获取水平对齐方式
//VerticalAlignment verticalAlignment =(((TextBlock)child).VerticalAlignment; //获取垂直对齐方式
}

如果(horizo​​ntalAlignment == Horizo​​ntalAlignment.Right)
{
Rect rec = new Rect(new Point(fw,y),child.DesiredSize);
child.Arrange(rec);
fw + = child.DesiredSize.Width;
}
其他
{
Rect rec =新Rect(新Point(x,y),child.DesiredSize);
child.Arrange(rec);
x + = child.DesiredSize.Width;
}
}
返回finalSize;
}
can change width or margin or thickness of the TextBox
thanks

Code :
protected override Size ArrangeOverride(Size finalSize)
{
double x = 0.0;
double y = 0.0;
double fw = finalSize.Width;
double TotRightWidth = 0.0;
double TotWidth = 0.0;

HorizontalAlignment horizontalAlignment = HorizontalAlignment;

foreach (UIElement child in this.Children)
{
if (child is TextBox)
{
horizontalAlignment = ((TextBox)child).HorizontalAlignment; //Gets horizontal alignment
TotWidth += child.DesiredSize.Width;
}
if (child is TextBlock)
{
horizontalAlignment = ((TextBlock)child).HorizontalAlignment; //Gets horizontal alignment
TotWidth += child.DesiredSize.Width;
}
if (horizontalAlignment == HorizontalAlignment.Right)
{
TotRightWidth += child.DesiredSize.Width;
}
}

double DiffWidth = (TotWidth - finalSize.Width) / Children.Count;
fw = finalSize.Width - TotRightWidth;

foreach (UIElement child in Children)
{
if (child is TextBox)
{
horizontalAlignment = ((TextBox)child).HorizontalAlignment; //Gets horizontal alignment
//VerticalAlignment verticalAlignment = ((TextBox)child).VerticalAlignment; //Gets vertical alignment
}
if (child is TextBlock)
{
horizontalAlignment = ((TextBlock)child).HorizontalAlignment; //Gets horizontal alignment
//VerticalAlignment verticalAlignment = ((TextBlock)child).VerticalAlignment; //Gets vertical alignment
}

if (horizontalAlignment == HorizontalAlignment.Right)
{
Rect rec = new Rect(new Point(fw , y), child.DesiredSize);
child.Arrange(rec);
fw += child.DesiredSize.Width;
}
else
{
Rect rec = new Rect(new Point(x, y), child.DesiredSize);
child.Arrange(rec);
x += child.DesiredSize.Width;
}
}
return finalSize;
}


这篇关于依赖项属性-文本框Horizo​​ntalAlignment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆