I got a weird problem with the rowSpan property of a grid.
I got a text that will take more than one line, so I told his stack spanel, RowSpan="2", so the text can be on multiple line, using Wrapping to, like this :
<StackPanel Grid.Row="0"
Grid.RowSpan="2"
Orientation="Horizontal">
<TextBlock x:Name="tbConfirm"
Text={Binding TextConfirm}
HorizontalAlignment="Center"
TextWrapping="Wrap"
VerticalAlignment="Center"
Grid.RowSpan="2"
FontSize="12">
</TextBlock>
</StackPanel>
The property TextConfirm contains a text that should take two lines (I tried with the text without binding to see if it fills.
But, despite my RowSpan and my TextWrapping, it still on one line, and don't understand why.
When I make a column span, it will take the number of column define, but why row span doesn't work?
There is in the Grid, 5 column(width auto) and 5 row(height auto) define.
If I want to have one textblock, one textbox, and another one textblock.
How can I tell to go in the new line automatically?
Undefined width for the two textblocks, and define width for the textbox.
This is just an example, I don't wrote it at the moment.
<StackPanel Grid.Row="0"
Grid.RowSpan="2"
Orientation="Horizontal">
<TextBlock x:Name="tbConfirm"
Text={Binding TextConfirm}
HorizontalAlignment="Center"
TextWrapping="Wrap"
VerticalAlignment="Center"
Grid.RowSpan="2"
FontSize="12">
</TextBlock>
<textBox width ="200"/>
<TextBlock x:Name="tbConfirm2"
Text={Binding TextConfirm2}
HorizontalAlignment="Center"
TextWrapping="Wrap"
VerticalAlignment="Center"
Grid.RowSpan="2"
FontSize="12">
</TextBlock>
</StackPanel>
Will this, go automatically at the new line if necessary? Or do I need to define lines and colums in the xaml?
Thank you.
I'm not sure if i understand the question correctly but to get the text wrapping you don't need to span accross multiple rows. From what i've read you have set the column width to auto so it will let the TextBlock "grow" freely. If you want to get the wrapping working you should define a fixed width either on the column or on the TextBlock itself (and keep the TextWrapping="Wrap").
try binding the width of the textblock to that of the parent as you wont want it to exceed that
<TextBlock x:Name="tbConfirm"
Text="asasddadsa adassdasd asdasdasdsa asdasdssd asdasdasjdahsjakdhksajdhjsaalskasj skjdsajkdhajhasaskjhdsahsakdasjdhsajdhasjdhkjsad"
HorizontalAlignment="Center"
Foreground="Black"
Width="{Binding ActualWidth, ElementName=stackPanelParent}"
TextWrapping="Wrap"
VerticalAlignment="Center"
FontSize="12">
</TextBlock>
I gave my own content to test. In case it overflows let me know.
Related
I am using a grid with single row. I have placed a single LongListSelector within that row. The ItemTemplate is as follows,
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<!--when we dont mention any of the height and width properties then control tries to first occupy the minimum height/width required and then it streches to the extent it is possible-->
<phone:LongListSelector Grid.Row="0" x:Name="CLASS1">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="25,20" Background="#FF616464" Width="Auto">
<TextBlock Text="{Binding title, Mode=TwoWay}" FontSize="40" Margin="20,20" Foreground="White" TextAlignment="Left" TextWrapping="Wrap" FontStyle="Normal" FontFamily="Segoe UI"/>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
Now I know that if we don't mention the width,MaxWidth values in the XAML file or in the properties, the Textblock above will stretch itself to the width it really needs to fit in the text. If there still some more width remaining it will expand till that extent. However I want to wrap the text of the Textblock. At the same time I want to utilize all the width available for stretching the TextBlock. So basically I want that the Textblock to stretch itself using the maximum width available and if the Text within it is still longer then I want to wrap it. Is there any solution to achieve this. I can use text wrap by setting constant value for width. As I want to deploy this application on different models then can I make it generic? Is there anyway to use parent's width?
All you have to do is to replace StackPanel with Grid.
<DataTemplate>
<Grid Margin="25,20" Background="#FF616464" Width="Auto">
<TextBlock Text="{Binding title, Mode=TwoWay}" FontSize="40" Margin="20,20" Foreground="White" TextAlignment="Left" TextWrapping="Wrap" FontStyle="Normal" FontFamily="Segoe UI"/>
</Grid>
</DataTemplate>
Result:
try this property
HorizontalContentAlignment="Stretch"
I have spent two hours researching how to avoid that my WPF TextBox Control grows when a long text has been typed in, but I have not been able to do it, even I have read some answers about it like these ones:
stopping-wpf-textbox-from-growing-with-text
wpf-allow-textbox-to-be-resized-but-not-to-grow-on-user-input
wpf-how-to-stop-textbox-from-autosizing
My code is the following:
<Grid>
<TextBox Margin="6,6,8,28" Name="textBox1" AcceptsTab="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" AcceptsReturn="True"/>
<CheckBox Content="Case sensitive" HorizontalAlignment="Left" Margin="7,0,0,2" Name="checkBox1" Height="16" VerticalAlignment="Bottom" />
</Grid>
One thing that I tried was:
MaxWidth={Binding ElementName=MyGrid, Path=ActualWidth}
But it did not work for me.
I also tried to add the following property to the Grid:
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Or
<Border x:Name="b" Grid.Column="1"/>
<TextBox Width="{Binding ActualWidth, ElementName=b}" ....... />
But it did not work either.
I need my control to grow when the user stretches the window, but not to when a long text is inserted.
Do you have a different answer for this problem that I might try?
UPDATE!
I have noticed something very strange: If I stretch manually the window, the textbox stops growing when a long text is inserted. That's fine. But I need to achieve this without having to stretch the window every time I run the program
Remove TextBox border and put it into ScrollViewer.
Try to do next:
<ScrollViewer MaxHeight={Binding ElementName=MyGrid, Path=ActualWidth} BorderThickness="0">
<TextBox Margin="6,6,8,28" Name="textBox1" AcceptsTab="True" TextWrapping="Wrap" AcceptsReturn="True"/>
</ScrollViewer>
I have a basic newbie binding question, which isn't necessarily directly related to the control used. Anyway, here's the thing: I have a DXGrid with a TotalSummary defined, which counts the rows of the grid.
<dxg:GridControl.TotalSummary>
<dxg:GridSummaryItem x:Name="grdCompleteCount" FieldName="ar_id" SummaryType="Count"/>
</dxg:GridControl.TotalSummary>
Now I'd like to display the count not at the bottom of the grid as it is done automatically, but would like to bind it to another element, say, a textblock. Something like this:
<TextBlock x:Name="statusBarGridCount"
Text="{Binding ElementName=grdCompleteCount, Path=Value}"
TextAlignment="Right"
Width="190" />
But this approach doesn't work as I'm not sure how to get to the value I'm looking for. What's wrong with the binding?
Ok, got it already, don't worry... :) Works that way:
<StatusBarItem Grid.Column="2" BorderThickness="1" Margin="1">
<TextBlock x:Name="statusBarGridCount"
Text="{Binding ElementName=grdList, Path=VisibleRowCount}"
TextAlignment="Right"
/>
</StatusBarItem>
I have this situation 1000 times already:
I have grid with 2 columns and many rows. Mostly i have TextBlock in first column and TextBox in second but not always. Now when I have 50 rows and I decide to move row 49 to the first row in grid, and I don't want to swap rows, I want to insert it to first place. Then I need to change values of Grid.Row and Grid.Column of all the rest of controls. This i driving me crazy. How can I make this easier?
You could use something like the markup extension i outlined in this question right from the beginning, if you then need to insert a new row that should be less trouble.
If you have formatted your xaml code well, you can hold Alt key to select and cut Grid.Row="x" in every line and hold Alt key to select and paste them one line down.
eg.
<TextBlock Grid.Row="0" Text="a"/>
<TextBlock Grid.Row="1" Text="b"/>
<TextBlock Grid.Row="2" Text="c"/>
<TextBlock Grid.Row="3" Text="d"/>
you want to move <TextBlock Grid.Row="3" Text="d"/> to the top. Just hold "Alt" key to select and copy:
Grid.Row="1"
Grid.Row="2"
Grid.Row="3"
Use Alt key again to paste them to replace the original
Grid.Row="0"
Grid.Row="1"
Grid.Row="2"
so you well get
<TextBlock Grid.Row="1" Text="a"/>
<TextBlock Grid.Row="2" Text="b"/>
<TextBlock Grid.Row="3" Text="c"/>
<TextBlock Grid.Row="3" Text="e"/>
Then just move <TextBlock Grid.Row="3" Text="d"/> to the top and change it Grid.Row to 0
It sounds like you'd be better off using a ListBox or ListView.
Let's say I have a simple TextBox next to a Label:
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Margin="3">MyLabel</Label>
<TextBox Margin="3" Width="100">MyText</TextBox>
</StackPanel>
...
</StackPanel>
This yields the following result:
As you can see, the base lines of MyLabel and MyText are not aligned, which looks ugly. Of course, I could start playing around with the margins until they match up, but since this is such a common requirement I'm sure that WPF provides a much easier and more elegant solution, which I just haven't found yet...
This behaviour is, I think, caused by the fact that the TextBox defaults to a vertical alignment of Stretch, which causes it to fill the available space and have the extra couple of pixels under the text. If you use this instead:
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label >MyLabel</Label>
<TextBox VerticalAlignment="Center" Width="100">MyText</TextBox>
</StackPanel>
</StackPanel>
... you should see a cleaner result.
What do you think?
<StackPanel Orientation="Horizontal">
<Label Margin="3" VerticalContentAlignment="Center">MyLabel</Label>
<TextBox Margin="3" VerticalContentAlignment="Center" Width="100">MyText</TextBox>
</StackPanel>
I achieved that look in Kaxaml with:
<StackPanel Orientation="Horizontal">
<Label Margin="3" VerticalAlignment="Center">MyLabel</Label>
<TextBox Margin="3" Width="100" VerticalAlignment="Center">MyText</TextBox>
</StackPanel>
I know that this is an old answer, but for here's an example for those who seek another way, where you don't need to rely on a fixed textbox width:
Instead of StackPanel, use a DockPanel and .Dock.
This proves to be very handy when used inside a Grid.
<DockPanel Grid.Column="2" Grid.Row="2">
<Label Content="SomeTitle:" DockPanel.Dock="Left"></Label>
<TextBox x:Name="SomeValueTextBox" VerticalAlignment="Center" DockPanel.Dock="Right"></TextBox>
</DockPanel>
This question is not as trivial as it looks and the accepted answers lacks details. If you try custom heights with the controls, you will see issues.
First, this is the correct implementation as answered by User7116.
<StackPanel Orientation="Horizontal">
<Label Margin="3" VerticalAlignment="Center">MyLabel</Label>
<TextBox Margin="3" Width="100" VerticalAlignment="Center">MyText</TextBox>
</StackPanel>
The tricky part is that there two level of vertical alignments here so understand how the alignments works.
When we specify alignment for a control, we are telling it how to position itself in the parent container (See documentation). So when we specify VerticalAlignment="Center"> to the TextBox we are telling it that this TextBox should appear vertically centered in parent stackpanel.
Now the actual text inside that TextBox could also use vertical alignment within that TextBox! This is the 2nd level and actually quite tricky and is answered here.
If you experiment with setting the Label's height above to say 50 as well, you will see they will not align again. This is because Label is now taking larger area and its text inside that area is not vertical aligned so it doesn't look aligned again.
The code for above is:
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Label Margin="3" VerticalAlignment="Center" Height="50">MyLabel</Label>
<TextBox Margin="3" VerticalAlignment="Center" Width="50" Height="50">MyText</TextBox>
</StackPanel>
Luckily when control height is default (like label control), it's just tall enough to contain the text so the inside alignment doesn't matter. But it comes into play if someone is setting custom heights for these controls and its better to understand how this works.