I know many people asked same question, but after searching I couldn't find my answer yet.
Here is problem, I just want to touch TabItem of TabControl.
So for this I can use MouseDown/MouseLeftButtonDown as I know.
I have adding MouseDown/MouseLeftButtonDown for each TabItem,
Even I have adding MouseDown/MouseLeftButtonDown on TabControl(On this case, it's just work on area, not tabs)
Problem is tabItem isn't opens event after left clicking.
I have adding this, it's just works when user touch label of tabitem, so when user click on tabItem(Not label) this isn't works too
<Style TargetType="TabItem">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<Label Content="{Binding}">
<Label.Style>
<Style TargetType="Label">
<EventSetter Event="MouseDown" Handler="TabItemMouseDown"/>
</Style>
</Label.Style>
</Label>
</DataTemplate>
</Setter.Value>
</Setter>
Related
I am making a ListView which loads users in from an Active Directory. To accomplish the brand/styling of the company I am developing the application for I would like to tweak some of the styling of the ListView element.
I have made it so the border of the headers in the Listview are transparent. In the editor in Visual Studio it looks how I want it to be, but when I look at the headers in the ListView in runtime I still get to see borders separating the headers (See image below).
https://i.gyazo.com/99dc8d60d6c5b2e1761456df685d850f.png
I have already tried Googling and I even went to the second page of the Google search result. Can you imagine?
Down here is the style I have used for the headers in my XAML file
<Style x:Key="ListViewHeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="BorderBrush" Value="Transparent"></Setter>
<Setter Property="IsHitTestVisible" Value="False"></Setter>
</Style>
What I want is to remove those borders separating the headers in my ListView element.
You can override the template of the GridViewColumnHeader
<Window.Resources>
<Style x:Key="GridHeader" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
<TextBlock Text="{TemplateBinding Content}" Padding="5"
Width="{TemplateBinding Width}" TextAlignment="Right" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<ListView>
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource GridHeader}">
</ListView.View>
</ListView>
Solution taken form here: Remove Separators in ListView Columns - WPF
I am working in WPF C# and noticed that my expander will expand and collapse when when I click on the button or header (words) of the expander but will not expand when I click on the part of the expander beside the header.
The weird part is that it will collapse the expander when clicking on the same spot beside the expander.
So does anyone know how to allow the entire expander to be clicked on to expand and collapse it?
Any help is greatly appreciated!
<Expander ExpandDirection="Down" Width="Auto" Padding="4" Expanded="Expander_Expanded">
<Expander.Style>
<Style TargetType="Expander">
<!--<Setter Property="IsExpanded" Value="{Binding XPath=#Name, Converter={StaticResource ExpandConverter}}" />-->
<Setter Property="IsExpanded" Value="{Binding XPath=#Expand}" />
<Setter Property="Header">
<Setter.Value>
<MultiBinding Converter="{StaticResource NameConverter}">
<Binding XPath="#Name" />
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="FontWeight" Value="Bold" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsExpanded,RelativeSource={RelativeSource Self}}" Value="True">
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
<ListBox Name="itemsList"
ItemsSource="{Binding XPath=UpgradeAction}"
ItemTemplate="{StaticResource dtListItemTemplate}"
SelectionChanged="listItems_SelectionChanged"
Style="{StaticResource styleListBoxUpgradeAction}"
ItemContainerStyle="{StaticResource styleListBoxItemUpgradeAction}"
MouseDoubleClick="itemsList_MouseDoubleClick">
</ListBox>
</Expander>
Have a look at the controltemplate perhaps? You could also use a click event and see if the mouse is on the header part and make it a reusable Behavior.
The problem was that the width of the expander was not set, so it was auto. Therefore it worked when it was closing because the width, when opened, is set to the entire expander. However, it didn't work while collapsed because when collapsed, the expander width is only set to as far as the header goes. Thus, I changed the width to a fixed value and it allowed me to press anywhere on the top part of the expander to open and close it.
I made a custom resource dictionary style for a TreeViewItem, but I am having difficulties with it.
<Style x:Key="StageTreeViewItem" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Setter Property="Foreground" Value="Gold"/>
<Setter Property="FontFamily" Value="ArialN"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<Grid>
<Image Name="PrimaryButtonImage" Source="pack://application:,,,/Images/TreeViewItem/TreeViewItem_Normal.png"/>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The content/header of the TreeViewItem does not exist. I put "Stage One" as Header of the TreeViewItem, but it doesn't show up. Also, if I add multiple tree view items on another, it does not expand at all.
Another thing:
How can I remove the highlights when I select the tree view item? I want it to be transparent even when I hover over it and even when I click it. I don't want anything to happen, but I just don't know how, I tried everything.
Your provided code is not making it clear how you're setting header of TreeViewItem.
For other part of the question, you can use Triggers for events happening in WPF forms. Also have a look at this link, as you'll have to define a template for changing background color on mouse hover.
IsMouseOver Trigger not working in WPF
I need to display a number in a square, centered horizontally and vertically.
When I tried to use a label for that purpose, it seemed like it ignored the centering completely. So I decided to use a grid and display a label on the grid as that centers perfectly.
I need to use a template as there's several themes available. From what I've found on the internet, I thought this ( ignoring the centering for now )
<ControlTemplate x:Key="ClockTemplate">
<Grid>
<Grid.Style>
<Style TargetType="Grid">
<Setter Property="Background" Value="White"/>
</Style>
</Grid.Style>
<Label>
<Label.Style>
<Style TargetType="Label">
<Setter Property="Foreground" Value="#376092"/>
</Style>
</Label.Style>
<ContentPresenter/>
</Label>
</Grid>
</ControlTemplate>
would be correct. Using it as follows:
<ContentControl Content="20" Height="64" Width="64" Template="{DynamicResource ClockTemplate}"/>
the content is not displayed tho, what am I doing wrong? Also, is there a better way to achieve my goal?
As per my understanding this is not the correct approach. Instead of creating ControlTemplate you have to write a Style for your control like below, also use StaticResource binding if possible. It is faster than Dynamic binding. Please not that, I have not mentioned the Label size inside the ControlTemplate. Please do it based on your needs
<Style x:Key="ContentControlStyle"
TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid Background="White">
<Label Foreground="#376092"
Width="200"
Height="100" Content="{TemplateBinding Content}">
</Label>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
bind your ContentControl with the newly created Style like below
<ContentControl Style="{StaticResource ContentControlStyle} ">
If your requirement is only to set some value in ContentControl, use Label instead and change the Style of the Label. Because ContentControl is heavy
The Problem:
I am creating a drag/drop interface that allows for moving items within a tab as well as from one tab to another. Right now I have the following Style that allows me to target the Label within a TabItem. Because of a conflict with a ResourceDictionary, I want to add the Event through the code behind. The problem is if I add the Event to the TabItem then it triggers not only on the tab itself but the contents of the tab.
The Question:
How would I go about wiring up the event in the code behind for the following?
<Style
x:Key="DragDropTi"
BasedOn="{StaticResource {x:Type TabItem}}"
TargetType="TabItem">
<Setter
Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<Label
Foreground="White"
Content="{Binding}">
<Label.Style>
<Style
TargetType="Label">
<EventSetter
Event="Drop"
Handler="TabItemDrop" />
<Setter
Property="AllowDrop"
Value="True" />
</Style>
</Label.Style>
</Label>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
The answer ended up being easier than anticipated. I just had to set the MenuItem.Headeras a Label instead of a string of the name and attach the Event to the Label
Label header = new Label();
header.Content = "Tab Name";
header.AllowDrop = true;
header.Drop += new DragEventHandler(TabItemDrop);
TabItem tabItem = new TabItem();
tabItem.Header = header;