WPF TreeViewItem dyanmic context menu - c#

I'm having some trouble with a databound TreeView in WPF, basically I want a context menu to be databound to an IEnumerable property on my TreeViewItem ViewModel, this is what I'm trying to do in the of each TreeViewItem:
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu ItemsSource="{Binding ContextMenu}" />
</Setter.Value>
but it gives me an exception when loading the xaml saying it can't set ContextMenu on System.Object or something along those lines.
Can anyone shed some light on this?
Thanks

declare it as resource in your style and then assign it to Value as StaticResource
<Style>
<Style.Resources>
<ContextMenu x:Key="contextmenustyle" ItemsSource="{Binding ContextMenu}" />
</Style.Resources>
<Setter Property="ContextMenu" Value="{StaticResource contextmenustyle}">
</Style>

Related

Lost previous combobox item style after applying style for cursor on hover

I want all clickable elements in the app to have hand cursor on hover. My problem is in comboboxes: all of them apply default (or what?) style when I set my custom style below (but hand cursor works!):
<Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
</Style.Triggers>
</Style>
<StackPanel Grid.Column="1" HorizontalAlignment="Right" Orientation="Horizontal">
<TextBlock Style="{StaticResource OptionTextBlockStyle}" />
<ComboBox x:Name="SortTypesComboBox" FontSize="14px" DisplayMemberPath="Description" ItemContainerStyle="{StaticResource ComboBoxItemStyle}">
</ComboBox>
</StackPanel>
The first image is what I want but with hand cursor on clickable items, the second one is what I actually have applying my comboboxitemstyle.
I don't know why I lost my previous style using this line ItemContainerStyle="{StaticResource ComboBoxItemStyle}. How can I save it and make right cursor on hover at the same time?
---UPG----
I've just added BasedOn="{StaticResource {x:Type ComboBoxItem}}" line and this didn't help me much. Now I have my items with wrong style and green lightning:
The way that you define your style overrides the default style for ComboBox. Instead, you should base your style on the default style using the BasedOn property.
<Style x:Key="ComboBoxItemStyle"
TargetType="ComboBoxItem"
BasedOn="{StaticResource {x:Type ComboBoxItem}}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
</Style.Triggers>
</Style>
The reason for referencing the default style with {x:Type ComboBoxItem} is that it is an implicit style, which gets applied automatically to each control of that type in scope unless a different style is assigned explicitly and the x:Key of an implicit style is their TargetType.

Wpf Custom ListBox with Rounded Border

I have extended ListBox control and wanted to give it rounded corner so I have applied styles as below and it works fine:
<control:MultiSelectionListBox SelectionMode="Extended" ItemsSource="{Binding Offerables,Mode=TwoWay}"
SelectedItemsList="{Binding SelectedOfferables, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name"
Grid.Row="6" Grid.Column="0" MaxHeight="150">
<control:MultiSelectionListBox.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"></Setter>
</Style>
</control:MultiSelectionListBox.Resources>
</control:MultiSelectionListBox>
but I have too many Listboxes and I don't wanted to add style in each and every control individually.
I wanted to define this style in app.xaml and wanted to reuse that design by name like Style = "{StaticResource RoundedListBoxStyle}".
I have tried like below in app.xaml but with no luck
<Style TargetType="ListBox" x:Key="RoundedListBoxStyle">
<Setter Property="Border" Value="10"></Setter>
</Style>
The member Border is not recognized or is not accessible.
Please guide me what I am doing wrong here.
Finally I managed to do that as Bradley Uffner mentioned in the comment Border is not the property of the ListBox and we cannot use it in style like that. I used Style.Resource and mentioned Style for Border elements as below:
<Style TargetType="ListBox" x:Key="RoundedListBoxStyle">
<Style.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5"></Setter>
</Style>
</Style.Resources>
</Style>
and it worked. :)

Set property in ContentPresenter child control

I want to set TextTrimming on TextBlock.
<Style TargetType="{x:Type dg.CellValuePresenter}">
<Setter Property="ContentTemplate" Value="{StaticResource Tmp}" />
</Style>
My template:
<DataTemplate x:Key="Tmp">
<ContentPresenter Content="{Binding}" >
<ContentPresenter.Resource>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
</Style>
</ContentPresenter.Resource>
</ContentPresenter>
</DataTemplate>
Content Presenter is XamTextEditor from Infragistic:
<Style TargetType="{x:Type igEditors:XamTextEditor}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igEditors:XamTextEditor}">
<TextBlock/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Setter from DataTemplate doesn't work. Do you know how to set this property?
How can I set TextTrimming property in Textblock of XamTextEditor cudtom style? I cannot do this in this style because it is used in other datatemplates where texttrimming must be turn off. So I tried to set it in DataTemplate but it not work (i see in snoop that it is set to none)
Unfortunately I cannot show more code because I don't have access to internet on dev and write down more code is time consuming :/
I'm not sure if I get your problem completely.
But If you are using explicit column definition. Then instead of CellvaluePresenter try create a EditorStyle For any column type(I've done it for string type column so the editor would be XamTextEditor)
<!--(xmlns:igWindows="http://infragistics.com/Windows")-->
<Style TargetType="{x:Type igEditors:XamTextEditor}" x:Key="DefaultXamDateTimeEditor">
<Style.Resources >
<Style TargetType="{x:Type igWindows:SimpleTextBlock}" >
<Setter Property="TextTrimming" Value="CharacterEllipsis" />
</Style>
</Style.Resources>
</Style>
And use this style as columns EditorStyle. It should work in XamDatagrid as this is working for me. Please post specific situation If I didn't get the question right.

Set Style of ComboBoxItem = null, but programmatically

I have in my XAML the following lines as Window.Resources:
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="pics/greenbutton.png" />
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="White" />
</Style>
In my Window are several ComboBoxes where this is good. But I have one, where it is disturbing, so I wanted to set the style to null. I already put a Style="{x:Null}" inside the XAML-ComboBox. That gives the ComboBox itself a good view, but not the open Box (i.e. the ComboBoxItems). I use a DataBinding inside the Code-Behind, so how can I delete the window-style for the ComboBoxItems?
You should add to ComboBox resources empty style with target type ComboBoxItem.
You can do this in the XAML like this:
<ComboBox x:Name="myComboBox" ...>
<ComboBox.Resources>
<Style TargetType="ComboBoxItem">
</Style>
</ComboBox.Resources>
...
</ComboBox>
Or you can do this in the code-behind using following code:
myComboBox.Resources.Add(typeof(ComboBoxItem), new Style(typeof(ComboBoxItem)));

TreeViewItem to notify ViewModel when it is hovered

I am currently using the code from this blogpost in order to have my TreeView highlight those items, which are currently hovered by the mouse. This is working as intended, however now I want the TreeViewItems to notify their attached ViewModels when they are hovered / not hovered.
However I'm at a loss on how I can achieve this. The corresponding XAML code looks like the following:
<Style TargetType="{x:Type TreeViewItem}">
<Style.Triggers>
<Trigger Property="Controls:TreeViewHelper.IsMouseDirectlyOverItem" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Stile.Triggers>
</Style>
How can I bind the property from my ViewModel, named TreeNodeModel.IsHovered to the TreeViewItem (or probably the attached dependency property IsMouseDirectlyOverItem) so that I can react on those changes from within my code?
All the examples I found via google only explained how the set the background color. Thanks in advance for your time on the probably trivial answer.
In your Style, try adding a Setter which binds IsMouseDirectlyOverItem to IsHovered, and use the OneWayToSource binding mode to push the value the right way:
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Controls:TreeViewHelper.IsMouseDirectlyOverItem"
Value="{Binding Path=IsHovered, Mode=OneWayToSource}" />
<Style.Triggers>
...
</Style>
EDIT: As IsMouseDirectlyOver is read-only, and read-only DPs can't be the target of any bindings, Fredrik Hedblad's PushBinding may be a possible workaround: OneWayToSource Binding for ReadOnly Dependency Property
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="pb:PushBindingManager.StylePushBindings">
<Setter.Value>
<pb:PushBindingCollection>
<pb:PushBinding TargetDependencyProperty="Controls:TreeViewHelper.IsMouseDirectlyOverItem"
Path="IsHovered" />
</pb:PushBindingCollection>
</Setter.Value>
</Setter>
<Style.Triggers>
...
</Style>

Categories