I need to know the height of a wrapped textBox. I am using this code:
MyView myView = new MyView();
myView.MyTextBox.Text = "my large text";
myView.UpdateLayout();
double myHeight = myView.MyTextBox.Actualheight;
No matter how large is the text, I always get 12.96 as actualheight.
If I do the same with a datagrid in which I am add new items, I am getting the right height, so I am wondering if sometimes a wrapped textbox has another behavior.
Thanks.
EDIT: i have realized that the size depends of the size of the font. At first I setted 8, but if I set 16, the actual height is the double too. So it seems that the height of the textBox depends of the font size, and it has not account if it is wrapped or not.
Running a simple test in XAML, when the text wraps, it causes the textbox ActualHeight to increase as expected as shown in the image below.
<StackPanel Orientation="Horizontal">
<TextBox Width="100" HorizontalAlignment="Left" Margin="10"
FontSize="10" Name="tbx1"
TextWrapping="Wrap" Text="Some short text"/>
<TextBlock Foreground="White" VerticalAlignment="Center"
FontSize="20"
Text="{Binding ActualHeight, ElementName=tbx1, Mode=OneWay}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="100" HorizontalAlignment="Left" Margin="10"
FontSize="10" Name="tbx2"
TextWrapping="Wrap" Text="Some text that is longer"/>
<TextBlock Foreground="White" VerticalAlignment="Center"
FontSize="20"
Text="{Binding ActualHeight, ElementName=tbx2, Mode=OneWay}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="100" HorizontalAlignment="Left" Margin="10"
FontSize="15" Name="tbx3"
TextWrapping="Wrap" Text="Short text"/>
<TextBlock Foreground="White" VerticalAlignment="Center"
FontSize="20"
Text="{Binding ActualHeight, ElementName=tbx3, Mode=OneWay}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="100" HorizontalAlignment="Left" Margin="10"
FontSize="15" Name="tbx4"
TextWrapping="Wrap" Text="Some text that is longer"/>
<TextBlock Foreground="White" VerticalAlignment="Center"
FontSize="20"
Text="{Binding ActualHeight, ElementName=tbx4, Mode=OneWay}" />
</StackPanel>
UpdateLayout doesn't actually cause anything to be rendered, it just prepares it. So until you render your view or window with myView.Show() or something equivalent, the textbox doesn't get rendered so the ActualHeight value doesn't get calculated.
Related
I have ListView and i inserted Slider and TextBlock in the same column:
<DataTemplate x:Key="MyDataTemplate2">
<Grid Margin="-7" >
<Slider Name="sliderColumn"
HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource SliderStyle}" Width="80"/>
<TextBlock Text="{Binding Path=Value, ElementName=sliderColumn, StringFormat={}{0}}" FontSize="11" Foreground="White"
VerticalAlignment="Center" HorizontalAlignment="Right" />
</Grid>
</DataTemplate>
And this is the result:
Bu when the Slider value changed it become unreadable:
Any suggestions how to fix it and make sure to display only integer numbers ?
Change your slider Like this by ading these two properties in slider TickFrequency and IsSnapToTickEnabled
<Slider Name="sliderColumn"
HorizontalAlignment="Left" VerticalAlignment="Center" Width="80" TickFrequency="1" IsSnapToTickEnabled="True"/>
I am currently sitting on a Silverlight-Listbox and have some
trouble getting my listBox right.
Its (visually) starts from the second item.
I have to scroll up to see the first one.
Why is this happening and how could I fix this?
<ListBox x:Name="ServingsList"
Foreground="White"
Background="#FFB88A8A"
SelectionChanged="servingSelected"
Margin="0,0,0,297">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="70" Width="432">
<Rectangle x:Name="Linie"
Fill="#FF8D8D8D"
HorizontalAlignment="Right"
Height="2"
StrokeThickness="0"
VerticalAlignment="Top"
Width="380"
Margin="0,-30,0,0" />
<TextBlock x:Name="ServingTitel"
TextWrapping="Wrap"
Text="{Binding name}"
FontSize="21.333"
Margin="50,-60,0,0" />
<Image x:Name="Ribbon"
HorizontalAlignment="Right"
Height="30"
VerticalAlignment="Top"
Width="151"
Source="/TEX/GrayRibbon.png"
Stretch="UniformToFill"
Margin="0,-60,0,0" />
<TextBlock x:Name="Kcal"
TextWrapping="Wrap"
Text="{Binding kalorien]}"
FontSize="18.667"
Height="23"
Margin="0,-92,8,0"
TextAlignment="Right" />
<Button Content="1"
Width="55" Height="55"
BorderThickness="3"
FontSize="18.667"
Padding="-1,-2,0,0"
Margin="-400,-87,0,0"
FontWeight="Bold"
Click="servingButtonClicked" />
</StackPanel>
</DataTemplate>
</ListBox>
You need to check your Margin values - in the main list you're putting 297 pixels of whitespace below your list box. Other elements have weird values too. Expression Blend sometimes messes with the Margins if you change from a StackPanel to a Grid.
e.g:
Margin="0,0,0,297">
My listbox XAML looks like this:
<ListBox x:Name="lstbxbProducts" SelectionChanged="lstbxbProducts_SelectionChanged_1" HorizontalAlignment="Left" Height="547" Margin="0,221,0,0" VerticalAlignment="Top" Width="1044" RenderTransformOrigin="0.600000023841858,0.5">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Width="80" VerticalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="{Binding prdnum}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
<TextBlock Text=" -" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Width="400">
<TextBlock Text="{Binding prddsc}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" Width="180">
<StackPanel>
<TextBlock Text="{Binding um_cod}" Name="txtblkUmcode" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
</StackPanel>
<StackPanel Width="20"></StackPanel>
<StackPanel Width="20">
<Image Source="/Images/Arrowselection.png" Tapped="Image_Tapped_1" VerticalAlignment="Center" ></Image>
</StackPanel>
</StackPanel>
<StackPanel Width="180">
<TextBlock Text="{Binding prcby_prc}" VerticalAlignment="Center" HorizontalAlignment="Center" ></TextBlock>
</StackPanel>
<StackPanel Width="100">
<TextBox Text="{Binding stdordqty, Mode=TwoWay}" VerticalAlignment="Center" TextAlignment="Center" HorizontalAlignment="Right" ></TextBox>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to set the text of Name="txtblkUmcode" present in my listbox from c# codebhind in this way, but no luck:
TextBlock txtBlk = new TextBlock();
txtBlk = lstbxbProducts.FindName("txtblkUmcode") as TextBlock;
txtBlk .Text = "test";
Please let me know how can I:
set the text of that textblock prsent inside a datatemplate of a my listbox ?
i want to change the text of that textblock of that single listbox item only ?
How can I do this? Please let me know.
You have to avoid this kind of programming practice because if the view changes - for example there comes a modified DataTemplate with other controls displaying that property(Label, TextBox, etc) - can break your application.
Sometimes there is no other solution.
See the answer here: http://msdn.microsoft.com/en-us/library/bb613579.aspx
I am binding a TextBLock with a string Property but whenever this Property contains the text "Audit", the actual text displayed on the UI is "Audi". For any other scenario, this works fine.
I am using .Net 4.0 and OS is win XP.
I am not sure if this is a bug but I can't get rid of this. If it is a bug, where do I report WPF bugs? Can anyone else also reproduce this issue? If yes, is there a workaround?
Edit: The Binding for the textblock is:
<TextBlock Padding="0,5,5,5" TextWrapping="Wrap" Text="{Binding TaskRoleMapping}" FontWeight="Bold" Foreground="White" FontFamily="Segoe UI" Margin="3,0,0,0" VerticalAlignment="Center"/>
I am sure it's not a problem with the textlength as much bigger text gets displayed alright.
Edit: The TextBlock is contained in a StackPanel
<StackPanel Height="40" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left" Width="Auto">
<ContentControl Content="ContentControl" Template="{DynamicResource vector_Summary}" Height="16"/>
<TextBlock Padding="5,5,0,5" Height="28"
Text="{Binding Path=[LABEL.MAIN.HEADER], Source={x:Static Util:Util.labelDictionary}}"
FontFamily="Segoe UI" FontSize="12"
Foreground="#FFFFFF" Margin="1,7,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="Auto"/>
<TextBlock Padding="0,5,5,5" TextWrapping="Wrap" Text="{Binding TaskRoleMapping}" FontWeight="Bold" Foreground="White" FontFamily="Arial" Margin="3,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
I like Kent Boogaart's comment/points. Basically 't' char is being cut off/out from the view. So change the font size to smaller, or remove "Bold" or introduce min size of the TextBlock, or it's parent and 't' in "Audi" will magically appear
I am trying to add different button controls inside the listbox dynamically for each listbox item along with the data while populating it. To achieve this, under the datatemplate tag, I have added new stack panel in which I placed button controls. Now, I am trying to find the particular stackpanel in which the buttons are being placed and then make it visible true or false depending on my condition.
So, I have tried to find the desired stack panel to get the control over it through looping the listbox items. However, I am getting "Null Reference Exception" while iterating the listbox items.
The following is my xaml code and the later is my xaml.cs code:
<ListBox x:Name="TripList" Height="465" HorizontalAlignment="Left" VerticalAlignment="Top" Width="456" Background="White" Foreground="Blue">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="0,0,0,4">
<StackPanel Orientation="Vertical" Width="456">
<StackPanel Orientation="Vertical">
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Width="370" FontSize="24" Text="{Binding PUDetails}"/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="24" Text="{Binding TripStatus}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<StackPanel VerticalAlignment="Top" Orientation="Horizontal" Width="300">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Conf: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding ConfNumber}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Est Do Tm: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding DOTime}"/>
</StackPanel>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Vertical">
<StackPanel x:Name="stacktest" Background="Azure" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Svc: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding TripService}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="PU: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding PURoute}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="DO: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding DORoute}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="Pax: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="18" Text="{Binding PaxDetails}"/>
</StackPanel>
</StackPanel>
</StackPanel>
<StackPanel x:Name="stknotchecked" VerticalAlignment="Top" Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="300" x:Name="btnaccepttrip" Content="accept trip"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnrejecttrip" Content="reject"></Button>
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnshowmap" Content="show map"></Button>
</StackPanel>
</StackPanel>
<StackPanel x:Name="stkaccepted">
<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btndirections" Content="directions"></Button>
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnflightinfo" Content="flight info"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btndetails" Content="details"></Button>
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnlogtimes" Content="log times"></Button>
</StackPanel>
<StackPanel>
<Button x:Name="btnstatus" Content="set status"></Button>
</StackPanel>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
private void addbtncontrols()
{
foreach (TripsList lst in TripList.Items)
{
ListBoxItem item = TripList.ItemContainerGenerator.ContainerFromItem(lst) as ListBoxItem;
Button stk = FindFirstElementInVisualTree<Button>(item);
var stkitem =(Button)stk.FindName("btndirections");
stkitem.Visibility = Visibility.Collapsed;
}
}
private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);
if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;
}
}
return null;
}
Could someone please let me know the way to reslove my issue. Thanks in advance..
Navigating the visual tree to locate these elements and set their visibility manually is the wrong way to go about solving this problem! I see you are using databinding. Why not expose various properties of type Visibility in the model or view model you are binding to the UI, you can then bind these to the Visibility property of the various UI elements in order to show / hide them.
As an aside, learn about Styles, you XAML has numerous repeated style properties which make it lengthy and hard to follow. You should be able to define a TextBlock style that is applied to all of your TextBlocks to remove much of the repeated code in your question.
From the above description, can you provide more information about this:
Where did you invoke addbtncontrol method? If you are responding to an event, let say: Add one more item, then it should be done inside ViewModel.
"your condition": What kind of condition? Can we model it in the Listbox item ViewModel?
I have implemented something the same: Listbox with dynamic items and custom template. Also provide the ability to add new item. So, If you give me more information on what you want to accomplish or better your view model, i can help.