Right justify InputGestureText og menuitem - c#

DO you have any idea on how to right justify a menuitems InputGestureText?
My menu's InputGestureText will always display left-justified just like this:
File
New...............Ctrl+N
Open.............Ctrl+O
Options.........Alt+Enter
Exit................X
What I'm trying to do is to display it like this:
File
New.......................Ctrl+N
Open....................Ctrl+O
Options.............Alt+Enter
Exit...............................X
I tried doing InputGestureText.PadLeft() but my font-family is proportional so it will still result to
File
New ............Ctrl+N
Open...........Ctrl+O
Options......Alt+Enter
Exit..................X
I tried to research some ways to make it possible but no luck.

You have to modify the style of the menu item.
It should be sufficient to copy it and just change the alignment on the element with the name
InputGestureText, we use a ContentPresenter instead of a TextBlock, but both should work.
<ContentPresenter
Grid.Column="2"
Margin="16,0,6,0"
x:Name="InputGestureText"
Content="{TemplateBinding InputGestureText}"
HorizontalAlignment="Right"
VerticalAlignment="Center"/>

You could probably change the Template of menu items via a Style so that the container of that section aligns its content to the right.

Related

Highlight part of text in textblock not working on all listboxItems

I'm trying to highlight part of text in a textblock from a listbox datatemplate which in turn is bounded to a property of a custom class by using a textbox to search the list for input text. But the problem is that only part of the items are highlighting (most of the ones visible) but when i maximize the window and try to input another character then suddenly all of them gets highlighted my guess where the problem might be is in this piece of code:
ListBoxItem listboxItemFound= (ListBoxItem)this.listBox1.ItemContainerGenerator.ContainerFromItem(TItem);
Since this method is returning a null when the items are not visible but the items are currently in the listbox. Somehow I guess the items listboxItem instances are not yet created until you scroll down or maximize to view more items.
XAML DataTemplate:
<DataTemplate>
<Grid Name="gridOfListbox" Height="25" Margin="0,2">
<DockPanel Name="dockpanelWithTxtBlock">
<TextBlock Name="textbloxk" DockPanel.Dock="Left" FontSize="15" TextAlignment="Center">
<Run Text="" /><Run Background="Yellow" Text="" /><Run Text="{Binding ProductID}" />
</TextBlock>
</DockPanel>
</Grid>
</DataTemplate>
If more code is needed just let me know.
Any help would be greatly appreciated!!
Also if there is any other better way of finding the listboxItem bounded to the custom Item just let me know. Thank you very much!
[Pic of problem] http://i.stack.imgur.com/HViag.png
One way to fix this is to set VirtualizingStackPanel.IsVirtualizing to false for your ListBox. This will cause all of the items to be created right away. The downside to this is if your ListBox has many items, your program will use more memory (since more items will be created), and could potentially run slower depending on the number of items.
A better solution to consider would be to have multiple DataTemplates for this - one without the highlight, and one with. You can set a DataTemplateSelector for your ListBox (using the ItemTemplateSelector property). The selector can choose which template to use based on if the item matches the search term or not.
The tricky part would be writing the template with the highlighted text. You could probably achieve that by having properties on the object the ListBoxItem is bound to for the text before the highlighted text, the highlighted text, and then the remaining text.

Most simple FrameworkElement to handle HorizontalAlignment and VerticalAlignment?

This is Silverlight.
Initial goal is to display a random element in a Popup with some VerticalAlignment and HorizontalAlignment. I do not want to use VerticalOffset or HorizontalOffset, because there is more to what I really want to do, including some complex bindings.
First attempt was:
<Popup>
<Button
Height="135"
HorizontalAlignment="Stretch"
VerticalAlignment="Bottom" />
</Popup>
Second attempt was:
<Popup
Height="135"
HorizontalAlignment="Stretch"
VerticalAlignment="Bottom">
<Button />
</Popup>
Both were a failure: the Button was always on Top and not Stretch (HorizontalAlignment and VerticalAlignment didn't work).
So I had the idea to encapsulate the element in a simple FrameworkElement:
<Popup>
<Border>
<Button
Height="135"
HorizontalAlignment="Stretch"
VerticalAlignment="Bottom" />
</Border>
</Popup>
And it is working.
But I had to use Border in this example, when I could have done it with Grid and many other FrameworkElement (but not with Canvas or Viewbox or Popup). I'd like to know what is the most simple, efficient and processor-friendly transparent FrameworkElement to encapsulate another element with working HorizontalAlignment and VerticalAlignment? Is it:
Border? (like the above example)
UserControl?
ContentControl?
ContentContainer?
some custom and basic MyFrameworkElement? (might need help for most basic implementation)
something else like Grid?
WPF controls come in two flavors: Ones that interact with users (like accept user clicks like a button, or display text like a text block) and containers that control placement and layout of the previous ones.
Container controls are usually designed to lay out their children in a specific manner. For example, Canvases lay out children by X, Y, Width & Height values. Each one has a specific use, so you must read the documentation or tutorials for these container controls and understand how each works in order to select the appropriate one for a task.
In your case, you want the button to fill all available space in the popup (it seems, it isn't that clear). I know that the Grid does this by default. So I would do the following:
<Popup><Grid><Button /></Grid></Popup>

Change text of textblock within a panoramaitem

I need to use two textblock within a single panorama item. I have used a grid to implement this.
Problem is that I need to change the text of the textblock programmatically.
<phone:PanoramaItem Header="Current Status">
<Grid >
<TextBlock x:Name="Spent" Margin="138,0,90,464" FontSize="40"/>
<TextBlock x:Name="Left" Margin="138,50,90,414" FontSize="40"/>
</Grid>
</phone:PanoramaItem>
Saying:
Spent.Text = Convert.ToString(total_spent);
Left.Text = Convert.ToString(total_left);
in the c# file gives an error.
Please tell me how to change the text of the individual textblocks :)
P.S:
I am an absolute beginner and almost completely self taught, so a simple answer will be useful
Thanks
You need to use Text property of the TextBlock to get/set the Text.
From MSDN : TextBlock.Text
Gets or sets the text contents of a TextBlock.
Try This:
Spent.Text = Convert.ToString(total_spent.Text);
Left.Text = Convert.ToString(total_left.Text);

Disable StackPanel Highlighting in XAML

I have found very little information about this matter. Know that I am a newbie to C# and WPF:
I have a stack panel defined in a XAML file as such :
<StackPanel Orientation="Vertical" >
<TextBlock Text="Locale: " VerticalAlignment="Center"/>
<ComboBox x:Name="comboLocale" Width="60" VerticalAlignment="Center" SelectionChanged="comboLocale_SelectionChanged"/>
</StackPanel>
I want to disable the highlighting that happens when I MouseOver the stack panel, which creates a blue color inside the StackPanel for some reason. I don't have any special style set up yet. Some threads talked about setting OverridesDefaultStyle to TRUE, but this didn't seem to change anything. Also, StackPanel do not have a ControlTemplate available, so most of the solutions I found couldn't be applied since they refer to a Button or TextBlock.
Any input on the matter would be greatly appreciated!
-Regards
StackPanels in general have no visual representation and are just layout containers which control placement of other elements. Given that you haven't set anything like Background on your StackPanel, it isn't what's causing the highlight you're seeing unless some other part of your XAML or code is modifying it. The behavior you describe sounds like the default behavior of a Button but without seeing more of your code it's hard to tell where the behavior is coming from.
Since you mentioned in a comment you've found out you're actually looking at the expected behavior of a Menu as your culprit. You'll just need to edit the MenuItem Control Template, more specifically the IsHighlighted that's causing your highlight. You'd likely find something like this helpful.
Or there's lots more various information found with a quick search for customizing a WPF Menu / MenuItem, hope this helps.

Drop shadow effect on a HyperLink

I can't seem to find how to add a drop shadow to a hyperlink in a flow document. Since HyperLink is not a UIElement it has no Effect property (all my googling led me to DropShadowEffect). It does have a TextEffects collection but I can't seem to figure out how to create a drop shadow with that. Ultimately what I would like to do is add a dropshadow to the background brush not to the text itself.
My requirement is pretty vague, I just have to make the focus appearance look better (i.e. tab focus and keyboard focus, not mouse over), and I thought a light drop shadow would do the trick, but I'm open to other suggestions.
(I don't have enough rep to upload a screenshot grr so here's a simulation)
There is a HyperLink here
I would like to add a drop shadow around the gray part (light blue in my app) not the text (as mentioned before).
I am pretty sure you can't do this on a per-inline basis. The way WPF provides effects is it applies them wholesale to a single object in the visual tree. Content elements like a Hyperlink are all combined into a single visual element in their parent (the FlowDocumentReader or comparable).
FlowDocument controls do have the ability to host child visual elements using the BlockUIContainer block content element, or InlineUIContainer inline content element. You can use this to apply a DropShadow to a single Hyperlink like so:
<FlowDocument>
<Paragraph>
<Run Text="This is a" />
<InlineUIContainer>
<TextBlock Background="#FFCDCDCD">
<TextBlock.Effect>
<DropShadowEffect ShadowDepth="3" Color="#FFC9C9C9" />
</TextBlock.Effect>
<Hyperlink><Run Text="hyperlink" /></Hyperlink>
</TextBlock>
</InlineUIContainer>
</Paragraph>
</FlowDocument>

Categories

Resources