How can I set the ChildWindow Title content from a style?
Setting the content to text is straightforward:
Setter Property="Title" Value="My Title Text"
How can I put a StackPanel or Image in the Title from the Setter? I know I could extract the entire style for the control and modify the Chrome but I would like to avoid having all that XAML to wade through just to change a small part.
<basics:ChildWindow.Title>
<StackPanel>
<TextBlock Text="Moo Title"/>
<Rectangle Fill="DarkCyan" Height="25" Width="25"/>
</StackPanel>
</basics:ChildWindow.Title>
Why do you want to use a setter in the first place?
Related
I'm creating a custom TextBox with a Title and I'm trying do it with a template.
The problem is that I don't want to have a textbox inside of textbox.
For example:
<TextBox.Template>
<ControlTemplate TargetType="{x:Type TextBox}">
<StackPanel Height="30" VerticalAlignment="Top" Background="green">
<TextBlock Text="Title"/>
<TextBox Text="{Binding Text, ElementName=txtBox}"/> <!-- How I can do it without this? -->
</StackPanel>
</ControlTemplate>
</TextBox.Template>
This way works like I want but I don't want to recreate the TextBox with binding.
Or is this the right way?
You're not putting "a textbox inside a textbox", you're putting it inside a template. Very different thing. There's still only one textbox, you're just extending its appearance.
If you want fundamental TextBox behavior then you have to use a TextBox in your template, there's not really any way around that. I suspect though that the real problem here isn't so much the TextBox as it is the explicit binding. If that's what you're trying to avoid then you can use a TemplateBinding to bind to the templated control's Text property instead:
<TextBox Text="{TemplateBinding Text}"/>
EDIT: just as a follow-up to this, TemplateBinding is one-way only. If you need two-way binding then use Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" instead.
I have a user control that shows a TextBox along with a small help icon.
My goal is to have a ToolTip pop-up, show some databound text and stay open when the mouse hovers over the help icon.
So, to that end I have created a HelpText dependency property in the user control allowing me to bind a help text string to the user control.
So, my user control looks something like this
<UserControl Name="textField" ...>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding ElementName=textField,Path=Text}"/>
<Image Source="{StaticResource Help.Icon}">
<Image.ToolTip>
<ToolTip Content="{Binding ElementName=textField,Path=HelpText}"/>
</Image.ToolTip>
</Image>
</StackPanel>
</UserControl>
This code does show the tooltip, except that it is empty! Also, the StaysOpen property does not make any difference as the tooltip shuts down after a few seconds.
Funny thing is that when I set the same binding directly on the Image control's ToolTip property the bound text is shown allright in the tooltip pop-up, however it still does not stay open:
<Image Source="{StaticResource Help.Icon}" ToolTip="{Binding ElementName=textField,Path=HelpText}">
So my to questions are:
How come the binding against the user control's HelpText dependency property does not work in the first code sample but does work in the second?
How do I make the ToolTip stay open or rather how do I make the ToolTip both stay open and show the databound text?
Thanks!
ToolTips are not part of the same VisualTree as the rest of your XAML, so the DataContext is not inherited the way you would expect it to be.
Writing ToolTip="{Binding SomeProperty}" will automatically set the ToolTip's DataContext to SomeProperty, however if you build a custom ToolTip you must do this yourself.
<ToolTip DataContext="{Binding PlacementTarget.DataContext,
RelativeSource={RelativeSource Self}}" ... />
This will bind the ToolTip's DataContext to the DataContext of whatever object the ToolTip is on.
To accomplish what you're trying to do, your <ToolTip> would probably look like this, since PlacementTarget would be your Image:
<!-- Could also use something like Tag if DataContext is actually used -->
<Image DataContext="{Binding ElementName=textField, Path=HelpText}"
Source="{StaticResource Help.Icon}">
<Image.ToolTip>
<ToolTip Content="{Binding PlacementTarget.DataContext,
RelativeSource={RelativeSource Self}}"/>
</Image.ToolTip>
</Image>
As for why it won't stay open, I'm not positive but it might be because the ToolTipService.ShowDuration property defaults to 5 seconds, and that probably overwrites the StaysOpen property.
You can try setting it to something higher, such as
<Image ToolTipService.ShowDuration="60000" ... />
Or you can try this workaround of using a Popup styled to look like a ToolTip instead. The code would probably look something like this:
<Popup PlacementTarget="{Binding ElementName=MyImage}"
IsOpen="{Binding IsMouseOver, ElementName=MyImage, Mode=OneWay}">
<TextBlock Text="{Binding ElementName=textField, Path=HelpText}" />
</Popup>
I'm trying to vertically center the content of a TextBox with the VerticalContentAlignment property but it seems to have no effect at all. The text stays at the top. Can anyone tell me how to do this?
Here's my code:
<TextBox Grid.Column="1"
Grid.Row="0"
Width="200"
Height="28"
VerticalAlignment="Center"
VerticalContentAlignment="Center" />
It is possible to make the TextBox center its text vertically. However, that does require you to reapply its ControlTemplate.
To do this:
Copy the Style and the ControlTemplate from the TextBox Styles and Templates page on MSDN to a suitable <UserControl.Resources> element. (This ControlTemplate is actually for a validation tooltip; the ControlTemplate we'll change is within the Style.)
Find the ScrollViewer element within the Style for the TextBox, and add a VerticalAlignment="Center" property to it.
Alternatively, you could add the property
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
to the ScrollViewer. This should allow you to set the vertical alignment of the contents of your TextBoxes using the VerticalContentAlignment property.
You can follow much the same approach if you wish to change the horizontal alignment of a TextBox's content as well.
The XAML code is correct, the following should be sufficient:
<TextBlock Text="Centered Text" VerticalAlignment="Center" />
Can you try that code outside your grid?
Check the attributes you defined on your Grid, this will probably cause the behaviour you have. Can you post the complete XAML code?
Is it possible in XAML to
Define a style for panel that creates a border for each panel?
Change the style of an ancestor item (i.e. the border background) when an item or it's child is selected?
i.e.
<StackPanel Style="{StaticResource WidgetStyle}">
<Label />
<Button />
<StackPanel>
<ListView />
</StackPanel>
</StackPanel>
I would like to have WidgetStyle define a (rounded) border and change the border color, e.g. if the ListView or Button is selected.
Thanks!
I'm afraid the answer to your first question is no! You cannot template Panel controls, they are lookless. What you could do in this. There are a few options, such as create a custom control that include a StackPanel. The answe to this question includes quite a few good ideas:
Changing WPF StackPanel template
For your second question, again no, you cannot style an ancestor based on a style change in a child by styling alone.
You could use ElementName binding to connect some properties of the elements together:
<Border Background={Binding ElementName=MyButton, Path=Tag>>
<StackPanel Style="{StaticResource WidgetStyle}">
<Label />
<Button x:Name="MyButton"/>
<StackPanel>
<ListView />
</StackPanel>
</StackPanel>
</Border>
In the above, the border background is bound to the Tag property of the button 'MyButton' you can then apply a style to the button which sets the Tag property and will thus change the border background.
I'm a noobie when it comes to WPF xaml so i'm hoping my question is so easy it can be answered in one line.
I'm looking for the best way to display an icon next to a block of text.
When a user hovers over the block of text or the icon i want to change the icon to another one.
Also, is it best practice to create one image with all my icons inside?? and move the background to the correct area?
One approach might be to bind the visibility of the image to the IsMouseOver property of the TextBlock, like this:
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis" />
</StackPanel.Resources>
<Image
Source="foo.jpg"
Margin="0 0 5 0"
Visibility="{Binding IsMouseOver,ElementName=text,Converter={StaticResource BoolToVis}"
/>
<TextBlock x:Name="text" Text="Mouse over me to show the image!" />
</StackPanel>
That's untested, but it should be sound. Let me know if it works for you.