夥計 我有StackPanel。我需要在那裏TextBlock與TextAlignment = TextAlignment.Center [中心的堆棧面板]和按鈕在右側邊緣小。 如何用wpf實現這樣的佈局。WPF。佈局問題
我如何添加按鈕的StackPanel,我的TextBlock沒有從中心移動。
夥計 我有StackPanel。我需要在那裏TextBlock與TextAlignment = TextAlignment.Center [中心的堆棧面板]和按鈕在右側邊緣小。 如何用wpf實現這樣的佈局。WPF。佈局問題
我如何添加按鈕的StackPanel,我的TextBlock沒有從中心移動。
嘗試是這樣的:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="txtCentered"
Grid.Column="0"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Here Is My Text Box" />
<StackPanel x:Name="stackButtons"
Grid.Column="1"
Orientation="Vertical"
VerticalAlignment="Center">
<Button x:Name="btnOne" Content="Button One" />
<Button x:Name="btnTwo" Content="Button Two" />
<Button x:Name="btnThree" Content="Button Three" />
</StackPanel>
</Grid>
注意,文本框覆蓋整個電網(ColumnSpan = 「2」),這樣它會在網格(的HorizontalAlignment =「中心絕對中心「)。第二列只有一個帶有按鈕的StackPanel(或Grid,或UniformGrid,或...)。
注:本設計的已知的缺點是,如果文本框是足夠大,網格足夠小的按鈕可以重疊文本框。必須小心避免這種情況,並將其作爲開發人員的練習。但是,如果要求TextBox是佈局的死點,則會出現此缺陷。
顯然,如果你想使用TextBlock而不是TextBox,你可以。你的問題和評論都提到了,最後我讀了「TextBox」。 :) – 2010-10-12 15:03:41
Grid.ColumnSpan =「2」 是我忘記的關鍵功能。非常感謝!!! – 2010-10-12 15:31:24
如何像:
<StackPanel>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="MyTextBlock" TextAlignment="Center"/>
<Button HorizontalAlignment="Right" Grid.Column="1">MyButton</Button>
</Grid>
</StackPanel>
編輯:
如果你想在文本塊是在網格的中心,你可以去除上面的ColumnDefinitions。但是,請注意,如果網格在此處變得足夠小,則該按鈕將與TextBlock重疊。
<StackPanel>
<Grid HorizontalAlignment="Stretch">
<TextBlock Text="MyTextBlock" TextAlignment="Center"/>
<Button HorizontalAlignment="Right">MyButton</Button>
</Grid>
</StackPanel>
請參閱編輯。看看這對你是否足夠有效。 – ASanch 2010-10-12 14:40:30
謝謝,但如果我想要這個網格的更復雜的變體佈局?這個沒有列的變體不是很靈活。如果我想要兩個或三個按鈕在右側。我認爲絕對的利潤並不好。 – 2010-10-12 14:47:22
如果您使用的是網格,爲什麼要使用StackPanel呢? – 2010-10-12 14:53:58
爲了避免將TextBlock和Button之間可能的重疊,就可以計算出左邊距爲使用值轉換器的中心旁到TextBlock結束按鈕。這就是說,我仍然喜歡@Wonko提供的答案,因爲它很簡單,很標準。
這裏是XAML:
<Window x:Class="TextBoxInCenter.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TextBoxInCenter"
Title="MainWindow"
Height="350" Width="525">
<Grid>
<Grid.Resources>
<local:CustomThicknessValueConverter x:Key="CustomThicknessValueConverter" />
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock
x:Name="CenterTextBox"
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="0"
Text="Text in Center"
HorizontalAlignment="Center"
VerticalAlignment="Center"
TextAlignment="Center"/>
<StackPanel
Grid.Column="1"
Grid.Row="0"
Orientation="Horizontal">
<Button
Margin="{Binding ElementName=CenterTextBox,
Path=ActualWidth,
Converter={StaticResource CustomThicknessValueConverter}}"
Height="23"
Content="Click me 1">
</Button>
<Button
Height="23"
Content="Click me 2">
</Button>
</StackPanel>
</Grid>
</Window>
這裏是值轉換器:
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Data;
namespace TextBoxInCenter
{
public class CustomThicknessValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Debug.WriteLine("Convert");
Thickness thickness = new Thickness(0, 0, 0, 0);
if (value != null)
{
double actaulwidth = (double)value;
thickness.Left = actaulwidth/2;
}
return thickness;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
你的意思是你想要的文本框對齊佈局的止點,並且按鈕文本框的權利(不影響居中)? – 2010-10-12 14:24:29
是的。我需要對齊的TextBox死對中 – 2010-10-12 14:36:17