Changing the Font of the string attached to the Bindings in WPF - c#

Here is the XAML
<ListView x:Name="duplicateVarsInDepXMLListView" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="100" Margin="362,360,0,0" VerticalAlignment="Top" Width="306">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name, StringFormat='Variable Name: {0}'}"/>
<TextBlock Text="{Binding Value,StringFormat='Value: {0}'}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This xaml displays a List of collection with Variables Names and the values of a collection. We have something like Foreground property to change the colour of the binding characters or FontWeight to make it Bold.
However i only want to Change the font of the Label e.g. VariableName and not to the actual binding.
Is this possible to achieve? Any suggestions

You can use Run elements inside a TextBlock and apply different formatting to different runs. You can bind to the FontFamily property of the Run to dynamically set the font:
<TextBlock>
<Run Text="Variable Name: " FontFamily="{Binding FontName}"/>
<Run Text="{Binding Name}"/>
</TextBlock>
If the text in the TextBlock is wrapped using TextWrapping="Wrap" the runs will wrap around as expected. This does not apply to the question but can make using Run elements superior to stacking TextBlock elements in a StackPanel.

Extract out seperate TextBlock and set FontFamily on that TextBlock. Wrap both TextBlocks in StackPanel and set the orientation to Horizontal -
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Variable Name: " FontFamily="Comic Sans MS"/>
<TextBlock Text="{Binding Name}"/>
</StackPanel>
<TextBlock Text="{Binding Value,StringFormat='Value: {0}'}"/>
</StackPanel>

You could do something like:
<StackPanel Orientation=Horizontal>
<TextBlock Text="Variable Name: "/>
<TextBlock Text={Binding Name} FontWeight="Bold"/>
</StackPanel>

Related

Long text in tooltip is not fully shown in UWP

I've defined a for a listbox item, and binded the text inside the tooltip to two properties of the object (name + description) but i have an issue that the text is being cut off
here is my tooltip:
<ToolTipService.ToolTip>
<StackPanel >
<StackPanel Orientation="Vertical">
<TextBlock FontSize="13">
<Bold>Name</Bold>
</TextBlock>
<TextBlock Text="{x:Bind name}"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="13" TextWrapping="Wrap">
<Bold>Description</Bold>
</TextBlock>
<TextBlock Text="{x:Bind description}"/>
</StackPanel>
</StackPanel>
</ToolTipService.ToolTip>
Now the thing is, if i bind the tooltip to a method that returns the name + description (Which is how it was previously, but was super ugly) it does show all the text, it was like this:
<ToolTipService.ToolTip>
<TextBlock Text="{x:Bind Description}"/>
</ToolTipService.ToolTip>
But I needed to style it to make it look better, so i've tried to do what was posted above.
I've already tried setting the Width/Height to super large values, didn't do anything.
any ideas?
The tooltip template probably has a default maximum width, which cuts off the TextBlock. To solve this, just add TextWrapping attribute:
<TextBlock TextWrapping="Wrap" Text="{x:Bind description}"/>
Now the tooltip text will wrap on multiple lines as necessary

How to bind items in a combobox in XAML

<TextBlock FontSize="20" Foreground="{x:Bind ????}">
<Run Text="{x:Bind Username}"></Run>
<Run Text=": "></Run>
<Run Text="{x:Bind Message}"></Run>
</TextBlock>
That is the TextBlock. My expectation is when the textblock get the string inside the combobox it will set the foreground through that string
<ComboBox
x:Name="CBBox"
PlaceholderText="Color"
Margin="10"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<x:String>Red</x:String>
<x:String>Yellow</x:String>
<x:String>Green</x:String>
<x:String>Purple</x:String>
<x:String>Back</x:String>
</ComboBox>
but I dont know what to bind any idea ?
Foreground="{Binding ElementName=CBBox,Path=SelectedItem}"
<ComboBox
x:Name="CBBox"
PlaceholderText="Color"
Margin="10"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<x:String>Red</x:String>
<x:String>Yellow</x:String>
<x:String>Green</x:String>
<x:String>Purple</x:String>
<x:String>Back</x:String>
</ComboBox>
<TextBlock FontSize="20" Foreground="{Binding ElementName=CBBox,Path=SelectedItem}">
<Run Text="{x:Bind Username}"></Run>
<Run Text=": "></Run>
<Run Text="{x:Bind Message}"></Run>
</TextBlock>
Usually, the best approach is to bind the ComboBox to the view model, and then bind the view model to the TextBlock.
However, it's possible to bind them directly, provided they're in the same scope (Page, UserControl, Template, etc.). You'll have to use {Binding} here, instead of {x:Bind}, since this you'll need a converter.
You can bind the Foreground property like this:
<TextBox Foreground="{Binding SelectedItem, ElementName=CBBox, Converter={StaticResource StringToColorConverter}" ... />
Now you'll need to add a ValueConverter, and get it to convert the selected string to a brush and return it.
Something like this should work:
var colorEnum = (Colors) Enum.Parse(typeof(Colors), value);
var color = new Color(colorEnum);
return new SolidColorBrush(color);

A ListBox inside a stackpanel doesn't work

I have these controls:
<StackPanel>
<TextBlock Text="Bill Benson"/>
<Image Source="/Assets/Images/BB.png"/>
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text=""/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Text="ends: 2015"/>
</StackPanel>
ListBox may have any number of items inside it. I want the whole page scroll from top to bottom, but it doesn't scroll down-up. How can I do that? I know the problem is because a scrollable control inside another scrollable one, but don't know how to fix it.
You need to assign the height to the listbox control, as it is taking an auto which means that the height keeps on increasing according to the count of items in it, so its impossible to access the items in the bottom of the control, So either give it a height or instead of stackpanel keep it in a grid with row definitions.
<StackPanel>
<TextBlock Text="Bill Benson"/>
<Image Source="/Assets/Images/BB.png"/>
<ListBox Height="200">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text=""/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock Text="ends: 2015"/>
</StackPanel>
Try specifying a height for your controls, especially StackPanel and ListBox, even just an "Auto" Height could work for testing. Also, rather than setting an ItemTemplate, which would be more useful if you had a List of items to bind the ListBox to, try some hardcoded values:
<ListBox>
<TextBlock Text="1"/>
<TextBlock Text="Test"/>
<TextBlock Text="Another"/>
<TextBlock Text="testing"/>
<TextBlock Text="Sample"/>
</ListBox>
Try to add more than the ListBox can show, and see if it scrolls.
Try a dock panel instead:
<DockPanel>
<StackPanel DockPanel.Dock="Top">
<TextBlock Text="Bill Benson"/>
<Image Source="/Assets/Images/BB.png"/>
</StackPanel>
<TextBlock DockPanel.Dock="Bottom" Text="ends: 2015"/>
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text=""/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DockPanel>
The ListBox will automatically expand to fill the space. Note: It has to be the last child in the DockPanel. (You can change this functionality using LastChildFill="False")

Accessing a TextBlock which is contained inside a ListBox

I have a textblock which is inside a listbox and I am trying to write an if statement which is dependant on the contents of this textblock. I am trying to get the data from the TextBlack which I have named "category1" however when I try to write my if statement I am getting a message which just says
"the name category1 does not exist in the current context"
I tired moving that TextBLock out of the ListBox and it works fine but wont work while its inside there. Does anyone know how to reference this textblock.
Here is the my XAML code
<ListBox x:Name="HINList" Margin="0,300,-12,0" ItemsSource="{Binding Details}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding HINNumber}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding CategoryLetter}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="category1" Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="{Binding Category2}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="{Binding Category3}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Assuming you're writing your if statement in the code behind file, wouldn't something like:
if(((WhateverTypeIsInDetailsCollection)HINList.SelectedItem).Category1 == something) {
// then do whatever you want
}
As Russell pointed out there is a category1 item for every entry in the list. I assume you wanted to do something with the selected item.
This is due to xaml namescopes. The names inside a DataTemplate are in a different namescope than outside, that's why you can't access them (what #Russell pointed is part of why it's done this way).
I think that you want to access that field for the "Category1" property on the selected item of the HINList ListBox that is bound to the Details collection. What you can do is set the binding on the Category1 to be two way, and bind the SelectedItem of the ListBox to a Detail item like so:
xaml:
<ListBox x:Name="HINList" ItemsSource="{Binding Details}"
SelectedItem={Binding SelectedDetailItem, Mode=TwoWay}>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding Category1, Mode=TwoWay}" TextWrapping="Wrap" .../>
<!-- the other fields -->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
code-behind
if(SelectedDetailsItem.Category1==...)
{
....
}
Hope this helps :)

Listbox extending off the page

I have a listbox inside of a stack panel inside of a border in a silverlight application and when ever I add anything to the listbox it increases in height so the scrollbar is never used and it extends beyond the boundaries of the border element. I have tried explicitly setting the height attribute of the listbox, the border and the stack panel and it still goes beyond this.
Here is my code:
<Border x:Name="articlePane">
<StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top">
<Button Content="Latest" MouseEnter="HandleRollInAnimation" MouseLeave="HandleRollOutAnimation" />
<Button Content="Pending" MouseEnter="HandleRollInAnimation" MouseLeave="HandleRollOutAnimation" />
<Button Content="Done" MouseEnter="HandleRollInAnimation" MouseLeave="HandleRollOutAnimation" />
</StackPanel>
<ListBox x:Name="articleList" Margin="5" Background="Transparent" ItemsSource="{Binding}"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Top" HorizontalAlignment="Center">
<Image x:Name="articleImage" />
<TextBlock x:Name="articleTitle" Text="{Binding Path=Title}" FontSize="18" FontWeight="Bold"
Margin="5"/>
<TextBlock x:Name="articleDate" Text="{Binding Path=Date}" FontSize="14" Foreground="Gray"
Margin="5"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Border>
I have a feeling that the StackPanel just keeps on adding height forever. I think in this instance you'd be better off with a Grid layout within your Border. Put the Button StackPanel in one height defined row, and the ListBox in an autosizing * height row. This way Grid will stick within the confines of your form.

Categories

Resources