WPF: Align TextBlock with different font sizes at top - c#

I need to align two text elements of different font sizes at the top line.
All I managed so far is either this :
Resulting from this code:
<Canvas VerticalAlignment="Center" HorizontalAlignment="Center" SnapsToDevicePixels="True">
<StackPanel x:Name="RemainingTimeDisplay" Orientation="Horizontal" VerticalAlignment="Top">
<StackPanel.Margin>
<MultiBinding Converter="{StaticResource CenterConverter}">
<Binding ElementName="RemainingTimeDisplay" Path="ActualWidth"/>
<Binding ElementName="RemainingTimeDisplay" Path="ActualHeight"/>
</MultiBinding>
</StackPanel.Margin>
<!--<Run FontSize="360" Text="{Binding RemainingTime.Minutes, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>
<Run FontSize="128" Text="{Binding RemainingTime.Seconds, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>-->
<TextBlock FontSize="360" Text="{Binding RemainingTime.Minutes, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>
<TextBlock FontSize="128" Text="{Binding RemainingTime.Seconds, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>
</StackPanel>
</Canvas>
Or:
<Canvas VerticalAlignment="Center" HorizontalAlignment="Center" SnapsToDevicePixels="True">
<StackPanel x:Name="RemainingTimeDisplay" Orientation="Horizontal" VerticalAlignment="Top">
<StackPanel.Margin>
<MultiBinding Converter="{StaticResource CenterConverter}">
<Binding ElementName="RemainingTimeDisplay" Path="ActualWidth"/>
<Binding ElementName="RemainingTimeDisplay" Path="ActualHeight"/>
</MultiBinding>
</StackPanel.Margin>
<TextBlock FontFamily="Roboto" VerticalAlignment="Top">
<TextBlock FontSize="360" Text="{Binding RemainingTime.Minutes, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>
<TextBlock FontSize="128" Text="{Binding RemainingTime.Seconds, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, StringFormat=D2, Converter={StaticResource IntAbsConverter}}"/>
</TextBlock>
</StackPanel>
</Canvas>
I tried also some more things, that I don't repeat here in code. I tried using "run" as a child of on single TextBlock for both text elements and I (actually started) tried without the TextBlock that encloses the other two.
So I tried a lot, I googled a lot - and I'm still not where I want to be.
Any hint would be really appreciated!
Thanks,
Marcus

Try the following:
<TextBlock FontSize="360"
Text="00"
LineHeight="360"
LineStackingStrategy="BlockLineHeight" />
<TextBlock FontSize="128"
LineHeight="360"
LineStackingStrategy="BlockLineHeight">
<Run Text="00" BaselineAlignment="Top" />
</TextBlock>

Related

Why is this tooltip still enabled?

I am working on a WPF app, and I am trying to make a tooltip conditionally enabled. It was being odd, and I realized that even when I explicitly set the tooltip to not enabled (ToolTipService.IsEnabled="False"), the tooltip still shows up. Can anyone figure out what is going on here?
<TextBlock Grid.Row="2"
Grid.Column="0"
VerticalAlignment="Center"
FontSize="15"
ToolTipService.IsEnabled="False"
Visibility="{Binding IsConnected, Converter={StaticResource BooleanToVisibilityConverter}}">
<fa:ImageAwesome Icon="{Binding Path=BatteryLevelIcon, UpdateSourceTrigger=PropertyChanged}"
Height="20"
Width="20"
Foreground="Green"
Visibility="{Binding IsConnected, Converter={StaticResource BooleanToVisibilityConverter}}" />
<ToolTipService.ToolTip>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}%">
<Binding Path="BatteryPercentage" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ToolTipService.ToolTip>
</TextBlock>
Set ToolTipService.ShowOnDisabled = false;
ToolTipService.ShowOnDisabled

DependencyProperty.UnsetValue using template and MultiBinding

Anyone knows why I receive the error: "DependencyProperty.UnsetValue" when I call my Command through the Template
This is my template:
<DataTemplate x:Key="MenuComboBoxItemTemplate" DataType="ComboBox">
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text="{Binding Text.Display}" />
<Button x:Name="RemoveButton"
Style="{StaticResource DeleteButton}"
DockPanel.Dock="Right"
ToolTip="Delete"
HorizontalAlignment="Right"
Padding="2"
Margin="3,0,0,0"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl},AncestorLevel=1}, Path=DataContext.RemoveMenuItemCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource MultiValueConverter}">
<Binding Path="Name" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type menus:MenuComboBox}}" />
<Binding />
</MultiBinding>
</Button.CommandParameter>
<Image Source="{dx:DXImageOffice2013 Image=Delete_16x16.png}" />
</Button>
</DockPanel>
</DataTemplate>
My combobox:
<menus:MenuComboBox
x:Name="MyItems"
Grid.Column="0"
Grid.Row="2"
Padding="6,3,5,3"
BorderThickness="1"
Text="{Binding MyItems, UpdateSourceTrigger=LostFocus}"
ItemTemplate="{StaticResource MenuComboBoxItemTemplate}"
ItemsSource="{Binding Menus[MyItems].Items}"
NewMenuItemCommand="{Binding AddMenuItemCommand}"
GotFocusCommand="{Binding GotFocusCommand}" />
I am stuck on it :(

Wpf - Speed ListBox

I've got a Wpf Application and I'm having some speed/time issues. I've got a Grid with 2 columns. The left column contains a panel with a ListBox, and when one of the items is selected it creates a new panel and sets it at the right column of the Grid. So I've got a fixed panel on the left and a changing panel on the right.
My right panel contains some details and a ListBox. When I keep changing the right panel (change selection of the left panel) it gets really slow and UI freezes. I was able to find the issue, and it was with my ListBox. It has a complex ItemTemplate and this could explain why it is slow.
My ListBox ItemTemplate:
<DataTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="{Binding ., Converter={StaticResource MyConverter}}">
<TextBox Style="{StaticResource MyStyle}" Text="{Binding Property, Converter={StaticResource MyConverter}}" HorizontalAlignment="{Binding ., Converter={StaticResource MyConverter}}" FontSize="10" Visibility="{Binding Property, Converter={StaticResource MyConverter}}"/>
<StackPanel Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="{Binding ., Converter={StaticResource MyConverter}}">
<Border Padding="8" CornerRadius="10,10,10,10" Background="{Binding ., Converter={StaticResource MyConverter}}">
<StackPanel Orientation="Vertical">
<TextBox Style="{StaticResource MyStyle}" Text="{Binding Property}" Visibility="{Binding ., Converter={StaticResource MyConverter}}" FontSize="14" FontWeight="DemiBold" TextWrapping="Wrap" HorizontalAlignment="{Binding ., Converter={StaticResource MyConverter}}"/>
<StackPanel Orientation="Vertical" Visibility="{Binding Property, Converter={StaticResource MyConverter}}" HorizontalAlignment="Center">
<Image Source="{Binding Property, Converter={StaticResource MyConverter}}" MaxWidth="800" MaxHeight="200" Visibility="{Binding Property, Converter={StaticResource MyConverter}}"/>
<Image gif:ImageBehavior.AnimatedSource="ImageSource" Width="200" MaxHeight="200" Visibility="{Binding Property, Converter={StaticResource MyConverter}}"/>
</StackPanel>
<TextBox Style="{StaticResource MyStyle}" Text="{Binding ., Converter={StaticResource MyConverter}}" FontSize="14" TextWrapping="Wrap" VerticalAlignment="Stretch" HorizontalAlignment="{Binding ., Converter={StaticResource MyConverter}}"/>
</StackPanel>
</Border>
</StackPanel>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="{Binding ., Converter={StaticResource MyConverter}}" Margin="0, 2, 0, 0" Visibility="{Binding Property, Converter={StaticResource MyConverter}}">
<Image Source="ImageSource" Margin="0,0,0,5" MaxHeight="10" VerticalAlignment="Center"/>
<TextBox Style="{StaticResource MyStyle}" Text="{Binding Property, Converter={StaticResource MyConverter}}" VerticalAlignment="Center" FontSize="10" TextWrapping="Wrap"/>
</StackPanel>
</StackPanel>
</DataTemplate>
Given this, what are some ways to speed things up?

MultiBinding and ContentPresenter

This is my code of ContentPresenter:
<ListBox ItemsSource="{Binding Items}" BorderThickness="0" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentPresenter x:Name="PART_ValueContainer">
<ContentPresenter.Content>
<MultiBinding>
<Binding Path="Value"/>
<Binding Path="ReadOnly"/>
</MultiBinding>
</ContentPresenter.Content>
<ContentPresenter.Resources>
<DataTemplate>
<TextBox IsReadOnly="{Binding Path=Content.ReadOnly, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
Text="{Binding Path=Content.Value, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
TextAlignment="Left"/>
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I trying to fill the Text from object Items.Value and property IsReadOnly from Items.ReadOnly.
It doesn't work, I know that is not good solution, but, how to make something like that?

Add a tooltip to a textbox in a grid in silverlight

<Grid>
<Canvas>
<TextBox Name="txt" IsReadOnly="True" Width="620" VerticalAlignment="Center"
Canvas.Left="340" Canvas.Top="5" Text="{Binding RowTitle2,Mode=OneWay}"
ToolTipService.ToolTip="{Binding Path=Text, RelativeSource={RelativeSource Self}}"
TextWrapping="NoWrap" HorizontalAlignment="Stretch"/>
</Canvas>
</Grid>
this way there is no tooltip, so how to add one ?
here i create dynamically records(txtboxes in the grid)
but on a similar example everything works just fine(no dynamilcall creation of txtboxes)
<Grid>
<Canvas>
<TextBox Name="txt" Margin="0,5,5,0" IsReadOnly="True" Width="620" VerticalAlignment="Center"
Canvas.Left="-200" Canvas.Top="-5"
ToolTipService.ToolTip="{Binding Path=Text, RelativeSource={RelativeSource Self}}"
TextWrapping="NoWrap" HorizontalAlignment="Stretch"/>
</Canvas>
</Grid>
What's the point here ? Its a bit confusing to use ReadOnly text box in a canvas which is located in a grid. Instead use a TextBlock which also improves performance, prevents memory leak, and looks better.
<Grid>
<TextBlock Margin="0,5,5,0"
VerticalAlignment="Center"
HorizontalAlignment="Stretch"
TextWrapping="NoWrap"
Text="{Binding RowTitle2}"
ToolTipService.ToolTip="{Binding Path=Text, RelativeSource={RelativeSource Self}}"
/>
</Grid>

Categories

Resources