<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);
Related
I'm binding an SqlMoney type to a TextBox in WPF and I'm trying to put digits after decimal point into superscript. This is what I've got so far:
<TextBlock>
<Run FontSize="50">1000</Run>
<Run BaselineAlignment="TextTop" TextDecorations="Underline" FontSize="26">00</Run>
</TextBlock>
Is there a simple way to this in WPF using something like StringFormat or I need to split it somewhere else in code and then bind?
Edit:
Ok, I might have explained it badly. This is actual implementation in code right now:
Model property
public SqlMoney Price { get; }
View
<TextBlock Text="{Binding Mode=OneWay, Path=Price}" />
Effect:
Effect - before and after
Is there way to make View handle splitting or I need to change implementation in Model or VM?
When you use binding, you can use StringFormat to do what you need. In your example there is no binding so no place for StringFormat.
Here is a StringFormat you can use when binding:
StringFormat={}{0:00.00}
Example of binding with StringFormat (assuming Superscript is a property of you DataContext):
<TextBlock>
<Run FontSize="50">1000</Run>
<Run BaselineAlignment="TextTop" TextDecorations="Underline" FontSize="26" Text="{Binding Superscript, StringFormat={}{0:00.00}}"/>
</TextBlock>
If you don't have yet the DataContext set up and you want a self contained example you could create a Resource and bind to it:
<TextBlock>
<TextBlock.Resources>
<sys:Double x:Key="Superscript">0</sys:Double>
</TextBlock.Resources>
<Run FontSize="50">1000</Run>
<Run BaselineAlignment="TextTop" TextDecorations="Underline" FontSize="26" Text="{Binding Source={StaticResource Superscript}, StringFormat={}{0:00.00}, Mode=OneWay}"/>
</TextBlock>
In this example I had to use OneWay binding because I'm binding to a static resource.
Result:
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
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>
I have a TextBlock with a tooltip that displays the same data, in case of truncation. However if the property that TextBlock.Text and the tooltip's text are bound to is empty (null or zero length string) the tooltip appears as a small empty box. Is there a way to hide this and show no tooltip in this case?
<TextBlock Text="{Binding Text}">
<util:ToolTipManager.ToolTip>
<TextBlock TextWrapping="Wrap" Text="{Binding Text}" />
</util:ToolTipManager.ToolTip>
</TextBlock>
I have tried using a StringToVisibilityConverter by adding Visibility="{Binding Text, Converter={StaticResource StringToVisConverter}}" to the TextBlock without any luck.
I also tried implementing the answer given Hide tooltip if binding is null but that seems specific to their set-up (or at least I haven't figured out how to adapt it successfully).
(ToolTipManager is from http://www.codeproject.com/Articles/36078/Silverlight-2-0-How-to-use-a-DataBinding-with-the, used to provide the data binding for the tooltip.)
Edit:
In response to the comments, here is the XAML I tried for the related question mentioned above:
<TextBlock Text="{Binding PointName}">
<local:ToolTipManager.ToolTip>
<Grid>
<TextBlock TextWrapping="Wrap" Text="{Binding PointName}"/>
<Rectangle Fill="Transparent" Visibility="{Binding PointName, Converter={StaticResource StringToVisConverter}}" />
</Grid>
</local:ToolTipManager.ToolTip>
</TextBlock>
And here is my String to Visibility converter code:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string visible = (string)value;
return (!String.IsNullOrWhiteSpace(visible) ? Visibility.Visible : Visibility.Collapsed);
}
I assume you are using Siverlight 4 as it is one of your tags.
In Silverlight 4, I am pretty sure that you don't need the ToolTipManager anymore.
You can just wrap the Rectangle and the TextBlock with a Grid, like this,
<Grid>
<TextBlock Text="{Binding PointName}"/>
<Rectangle Fill="Transparent" Visibility="{Binding PointName, Converter={StaticResource BooleanToVisibilityConverter}}" ToolTipService.ToolTip="{Binding PointName}"/>
</Grid>
UPDATE:
<Grid>
<TextBlock Text="{Binding PointName}"/>
<Rectangle Fill="Transparent" Visibility="{Binding PointName, Converter={StaticResource BooleanToVisibilityConverter}}">
<ToolTipService.ToolTip>
<TextBlock TextWrapping="Wrap" Text="{Binding PointName}"/>
</ToolTipService.ToolTip>
</Rectangle>
</Grid>
If you are using Silverlight 5, the following seems to work:
<ToolTipService.ToolTip>
<ToolTip Visibility="{Binding WhatDeterminesTooltipVisibility}">
<Border Background="Azure" Width="100" />
</ToolTip>
</ToolTipService.ToolTip>
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 :)