Horizontalalalignment=Stretch, MaxWidth, and Left aligned at the same time?
Wydaje się, że to powinno być łatwe, ale jestem zakłopotany. W WPF chciałbym pole tekstowe, które rozciąga się na szerokość rodzica, ale tylko na maksymalną szerokość. Problem w tym, że chcę, żeby to zostało uzasadnione w rodzicu. Aby go rozciągnąć, musisz użyć Horizontalalalignment= "rozciągnąć", ale wtedy wynik jest wyśrodkowany. Eksperymentowałem z ustawianiem poziomym, ale nic z tego nie wynika.
Jak sprawić, aby niebieskie pole tekstowe rosło wraz z rozmiarem okna, ma maksymalną szerokość 200 pikseli i ma być usprawiedliwione?
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<TextBox Background="Azure" Text="Hello" HorizontalAlignment="Stretch" MaxWidth="200" />
</StackPanel>
</Page>
Co to za sztuczka? 6 answers
Możesz ustawić HorizontalAlignment
na lewo, ustawić MaxWidth
, a następnie bind Width
naActualWidth
elementu nadrzędnego:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Name="Container">
<TextBox Background="Azure"
Width="{Binding ElementName=Container,Path=ActualWidth}"
Text="Hello" HorizontalAlignment="Left" MaxWidth="200" />
</StackPanel>
</Page>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-26 21:38:04
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MaxWidth="200"/>
</Grid.ColumnDefinitions>
<TextBox Background="Azure" Text="Hello" />
</Grid>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-09-28 01:05:08
Obie odpowiedzi zadziałały na podany przeze mnie problem-dzięki!
W mojej prawdziwej aplikacji próbowałem jednak ograniczyć panel wewnątrz Przewijarki i metoda Kenta nie poradziła sobie z tym zbyt dobrze, z jakiegoś powodu nie pofatygowałem się wyśledzić. Zasadniczo elementy sterujące mogły wykraczać poza ustawienie MaxWidth i pokonały mój zamiar.
Technika Nira działała dobrze i nie miała problemu ze Scrollviewerem, chociaż jest jedna drobna rzecz, na którą należy uważać. Ty chcesz mieć pewność, że prawy i lewy marginesy na polu tekstowym są ustawione na 0, bo inaczej wejdą w drogę. Zmieniłem również powiązanie, aby używać ViewportWidth zamiast ActualWidth, aby uniknąć problemów podczas wyświetlania pionowego paska przewijania.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-11-11 11:26:27
Możesz użyć tego dla szerokości Twojego Datatematplate:
Width="{Binding ActualWidth,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollContentPresenter}}}"
Upewnij się, że Twój root DataTemplate ma Margin="0" (możesz użyć jakiegoś panelu jako roota i ustawić margines na dzieci tego roota)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-26 08:25:00
Użyłbym SharedGroupSize
<Grid>
<Grid.ColumnDefinition>
<ColumnDefinition SharedGroupSize="col1"></ColumnDefinition>
<ColumnDefinition SharedGroupSize="col2"></ColumnDefinition>
</Grid.ColumnDefinition>
<TextBox Background="Azure" Text="Hello" Grid.Column="1" MaxWidth="200" />
</Grid>
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-03 19:50:22
Może nadal mogę pomóc komuś, kto zadaje to pytanie, ponieważ jest to bardzo stary problem.
Potrzebowałem tego, jak również i napisał zachowanie, aby zająć się tym. Oto zachowanie:
public class StretchMaxWidthBehavior : Behavior<FrameworkElement>
{
protected override void OnAttached()
{
base.OnAttached();
((FrameworkElement)this.AssociatedObject.Parent).SizeChanged += this.OnSizeChanged;
}
protected override void OnDetaching()
{
base.OnDetaching();
((FrameworkElement)this.AssociatedObject.Parent).SizeChanged -= this.OnSizeChanged;
}
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
this.SetAlignments();
}
private void SetAlignments()
{
var slot = LayoutInformation.GetLayoutSlot(this.AssociatedObject);
var newWidth = slot.Width;
var newHeight = slot.Height;
if (!double.IsInfinity(this.AssociatedObject.MaxWidth))
{
if (this.AssociatedObject.MaxWidth < newWidth)
{
this.AssociatedObject.HorizontalAlignment = HorizontalAlignment.Left;
this.AssociatedObject.Width = this.AssociatedObject.MaxWidth;
}
else
{
this.AssociatedObject.HorizontalAlignment = HorizontalAlignment.Stretch;
this.AssociatedObject.Width = double.NaN;
}
}
if (!double.IsInfinity(this.AssociatedObject.MaxHeight))
{
if (this.AssociatedObject.MaxHeight < newHeight)
{
this.AssociatedObject.VerticalAlignment = VerticalAlignment.Top;
this.AssociatedObject.Height = this.AssociatedObject.MaxHeight;
}
else
{
this.AssociatedObject.VerticalAlignment = VerticalAlignment.Stretch;
this.AssociatedObject.Height = double.NaN;
}
}
}
}
Wtedy możesz go używać tak:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Label" />
<TextBox Grid.Column="1" MaxWidth="600">
<i:Interaction.Behaviors>
<cbh:StretchMaxWidthBehavior/>
</i:Interaction.Behaviors>
</TextBox>
</Grid>
I wreszcie, aby zapomnieć o użyciu przestrzeni nazw System.Windows.Interactivity
, aby użyć zachowania.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-19 12:44:53