I'm trying to bind the header text of aGroupBox to a property and display it using StringFormat.
The first part works and it returns the text as expected. But I want the final text to be formatted. For example when I return cm I want it to be displayed as Foundation Height (cm), but the code below only shows cm.
<GroupBox Header="{Binding CurrentTargetUnit,
Converter={StaticResource QuantityToTextConverter},
ConverterParameter={x:Static enumerations:Quantity.Length},
StringFormat='Foundation Height ({0})'}">
</GroupBox>
Try without quotes
....
StringFormat=Foundation Height ({0})}">
UPDATE
As you have used binding for header you should use HeaderStringFormatinstead.
....
HeaderStringFormat=Foundation Height ({0})}">
Read the documentation for more
I had the same issue. I found my answer here.
<GroupBox>
<GroupBox.Header>
<TextBlock Text="{Binding CurrentTargetUnit,
Converter={StaticResource QuantityToTextConverter},
ConverterParameter={x:Static enumerations:Quantity.Length},
StringFormat=Foundation Height ({0})}">
</GroupBox.Header>
</GroupBox>
You also need to remove the quotes from the StringFormat. Also, in the link above, it shows placing "{}" before anything in the StringFormat. If I recall correctly, you only need that if the place holder is first: StringFormat={}{0:C}
There are a limited amount of elements that will allow StringFormat Binding, TextBlock being one and GroupBox headers do not.
Try this instead:
StringFormat={}Foundation Height ({0})}">
Related
I'm a little surprised to see that this isn't a feature of the AutoSuggestBox but, I'm trying to configure an AutoSuggestBox to bold any matched text from what gets pulled up.
So it would look like:
[ jay ]
jaydeflix
jaydeflixutil
tommyjay
If anyone has sample code, I'll gladly look at it, but I'll even gladly take a pointer at what to dig into (I'm self-taught, so I'm used to digging, just my binggle-fu is coming up short on this one).
The suggestion list is actually a ListView of AutoSuggestBox. More details please see AutoSuggestBox styles and templates. To define a custom look for each item in the list, use the ItemTemplate property of AutoSuggestBox, for more details about this please check the Text changed section.
The TextBlock can be set with paticial bold text with <Run> tag that you can try to use inside the template. For example:
<AutoSuggestBox
x:Name="asb"
... >
<AutoSuggestBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run FontWeight="Bold" Text="{Binding QueryString}"></Run>
<Run Text="{Binding DisplaySpare}"></Run>
</TextBlock>
</DataTemplate>
</AutoSuggestBox.ItemTemplate>
</AutoSuggestBox>
For more about how to contact the string to display, set the display order and bind to ItemsSource please try it yourself. This depends on the original source and your special requirements.
I do not manage to properly set the width of my XAML ToggleSwitch.
My code is as follows:
<Controls:ToggleSwitch OnLabel="True"
OffLabel="False"
IsEnabled="{Binding CheckValueEnable}"
IsChecked="{Binding CheckValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="250" >
</Controls:ToggleSwitch>
Whatever Width value I set, there is a too big spacing between the text (true / false) and the visual toggle button.
How can I decrease this spacing?
I would guess your issue (over exaggerated a little) is something like this:
{O }___________True
What you want is:
{O }__True
Without knowing the control it is a bit difficult but i would guess you only option is to modify the ToggleSwitch's ControlTemplate to define the content presenter position
EDIT:
Further to that, you might find the following article useful as it describes the process of editing controls in XAML
http://docs.telerik.com/devtools/wpf/styling-and-appearance/styling-apperance-editing-control-templates
In my WPF app we are using an adorner for displaying validation messages, in the particular case there is a single row grid that has multiple controls some of which have validation. The problem I'm having is that I want to force the width of the error message control to be the same as the grid but can't seem to find a way to reference that grid from the adorner template. Here is a sample of what I tried:
<ControlTemplate x:Key="Local_TopAdornedTemplateWide">
<StackPanel>
<AdornedElementPlaceholder x:Name="adornedElement"/>
<TextBlock MaxWidth="{Binding Path=ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType=Grid}, ElementName=adornedElement}"
TextWrapping="Wrap"
Text="{Binding Converter={StaticResource Local_ValidationErrorMessageConverter}}"
Style="{DynamicResource Error_Text}"
Padding="2 1 0 0"
Visibility="{Binding ElementName=adornedElement, Mode=OneWay, Path=AdornedElement.IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
/>
</StackPanel>
</ControlTemplate>
This causes the application to crash with an XamlParseException.
Ideally the solution would not be specific to a grid so that it would get the width of any container type, but for now grid is the only use case.
Edit:
Here is an example of another template we use in the application; this template would not work for my case as it would limit the error to be the width of a single column of the aforementioned grid:
<ControlTemplate x:Key="Local_TopAdornedErrorTemplate">
<StackPanel>
<AdornedElementPlaceholder x:Name="adornedElement"/>
<TextBlock MaxWidth="{Binding ElementName=adornedElement, Path=ActualWidth}"
TextWrapping="Wrap"
Text="{Binding Converter={StaticResource Local_ValidationErrorMessageConverter}}"
Style="{DynamicResource Error_Text}"
Padding="2 1 0 0"
Visibility="{Binding ElementName=adornedElement, Mode=OneWay, Path=AdornedElement.IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
/>
</StackPanel>
</ControlTemplate>
Using snoop I captured the following two screenshots (I could not take one of the full stack to prevent posting anything proprietary)
This shot shows the grid I mentioned previously, within this it is the FinancialTextBox item that is being adorned
This shot shows two things, the item selected in blue is the highest ancestor of the grid in the previous shot, the yellow highlight is the Textbox from the content template
With those two it seems to be apparent that (based on information from Contango's answer) the two items aren't not in the same visual tree which would lead me to believe my question is not possible. However the second template I added (which does work) points that at least some visual information from the adorned element lives on in the place holder.
So now my question boils down to a) does this information include the parent of the adorned element and b) how can this be accessed via a binding on a different element?
This ended up being a lot simpler than the path I was trying to go down.
I was doing some reading on the AdornedElementPlaceholder class and came across this entry on MSDN and noticed that the class actually has a property called parent, with that I tried the following binding and it works perfectly:
MaxWidth="{Binding ElementName=adornedElement,
Mode=OneWay,
Path=AdornedElement.Parent.ActualWidth}"
WPF is quite powerful and flexible.
You can bind any property in any XAML tag to any property in any other XAML tag.
For example, you could write a test app that binds the Text property of an input box to the Text property of a label, so as you type something into the text box, the label would change automatically (assuming you use UpdateSourceTrigger=PropertyChanged). This is a direct XAML to XAML binding, with no C# in sight.
Similarly, you could bind the width of your error box to the width of the parent control, whatever that may be.
Google RelativeSource and AncestorType, this is a great link:
http://druss.co/2013/10/wpf-binding-examples/
See if you can grok how the Visual Tree and Logical Tree works in WPF, once you understand that, you will understand more of how binding works.
I'd also recommend using the free tool Snoop to look at the Visual Tree. XAML Spy is excellent, but not free.
Snoop can tell you if there is anything that has a bad binding at runtime (you set the filters up, and it will list all bad bindings).
You can use Snoop to get the full XAML path of your source (the XAML you wrote above), then get the full XAML path of the target (i.e. the ActualWidth of your Grid), then compare them: it may be quickly apparent that one is not the ancestor of the other, as they are on different branches of the visual tree, or that there is some other issue which is preventing a simple walk up the visual tree from working.
If you just want to get something working, as a proof of concept, try naming the target XAML grid using x:Name, and reference it by name instead of AncestorType.
I have need to programmatically create a TextBlock inside a WrapPanel. This TextBlock will act like a heading, therefore I don't want anything to appear to right of the TextBlock. Is there a better way to max out the Width of the TextBlock without doing something like;
myTexblock.Width = 1000000;
Thanks
I think a better solution than putting your header in your WrapPanel is to place the header and WrapPanel in a StackPanel
Something similar to this:
<StackPanel>
<TextBlock Text="Some Header Text"
HorizontalAlignment="Stretch" />
<WrapPanel>
<Button Content="Placeholder" />
<Button Content="Also holding a place" />
</WrapPanel>
</StackPanel>
This gives the same visual effect as what you described without the sloppy property setting.
I currently have a list of objects in which my RadGridView's ItemsSource is set to. When the property "DoNotContact" of the object in the list has been set to True, I want to hide the information in the cell that contains a Phone number within my RadGridView. As you can see in my XAML, I'm setting the Visibility property within the TextBlock like so:
<telerik:GridViewDataColumn Header="Evening" DataMemberBinding="{Binding Path=EveningPhone}" Width="75" SortMemberPath="EveningPhone">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Visibility="{Binding Path=DoNotContact, Converter={StaticResource BoolToVisibilityConverter}}">
<Hyperlink Click="MakeEveningCallHandler">
<TextBlock Text="{Binding Path=EveningPhone}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>
When attempting to debug it, the Converter is never hit and although I can see the property "DoNotContact" has been set, the phone number still shows. The converter itself works fine as I've used it in other occasions. Again I only want to hide the information WITHIN the cell for the "Evening" Property, not the actual column itself. Any Ideas what's going wrong here? Thanks a bunch!
The code you provided works for me!