I have this ListViewItem trigger:
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="Gray"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Height" Value="50"/>
</MultiDataTrigger>
When I am selecting my ListViewItem, this item becomes larger so I can show another elements.
Now I want to implement a behavior that after each click on a ListViewItem this item will change from selected to not selected, so after each click my ListViewItem changes its height to 50 and after another click back to 22 (the default size).
I subscribed to an PreviewMouseLeftButtonDown event:
private void listView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
}
My question is: what do I need to write into this event handler?
You could handle the PreviewMouseLeftButtonDown event for the ListViewItem container:
<ListView x:Name="listView">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
<Style.Triggers>
<MultiDataTrigger>
...
</MultiDataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
</ListView>
...something like this:
private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ListViewItem lvi = (ListViewItem)sender;
if (lvi.IsSelected)
{
listView.SelectedItems.Remove(lvi.DataContext);
e.Handled = true;
}
}
I put this EventSetter after my Style (this style is in another file) and got this: 'ResourceDictionary' root element requires a x:Class attribute to support event handlers in the XAML file ...
If you define the ItemContainerStyle in a ResourceDictionary, you need to add a code-behind file to the ResourceDictionary. This is an easy thing to do:
Is it possible to set code behind a resource dictionary in WPF for event handling?
The other option would be to define the Style with the EventSetter inline in your view and base it on the Style with the MultiDataTrigger that you have defined in the ResourceDictionary:
<ListView x:Name="listView">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{StaticResource YourOtherStyleInTheResourceDictionary}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
Related
I know that already some questions exist but I can not fix my problem with them.
Problem: I try to change a image with a data template but just the default image is visible.
Code:
My xaml code is like this:
<Window.Resources>
<DataTemplate x:Key="MultiTemplate">
<Image Height="17" Width="17">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="{svg2Xaml:SvgImage VideoControllerTester;component/Resources/Start.svg}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding MultiTrigger}" Value="start">
<Setter Property="Source" Value="{svg2Xaml:SvgImage VideoControllerTester;component/Resources/Start.svg}"/>
</DataTrigger>
<DataTrigger Binding="{Binding MultiTrigger}" Value="stop">
<Setter Property="Source" Value="{svg2Xaml:SvgImage VideoControllerTester;component/Resources/Stop.svg}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</DataTemplate>
</Window.Resources>
<ContentControl ContentTemplate="{DynamicResource MultiTemplate}"/>
In the code behind I set MultiTrigger = "start" or "stop".
Question: Can I show the images with the content control? Or I do some dumb stuff with the data template?
Edit:
public string MultiTrigger
{
get { return _multiTrigger; }
set
{
_multiTrigger = value;
RaisePropertyChanged();
}
}
Assuming that there is a MainViewModel class with a MultiTrigger property (which btw. is a strange property name), you would assign an instance of the view model class to the MainWindow's DataContext, either in code behind:
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
Or in XAML:
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
Then you would declare the Image Style as a resource:
<Window.Resources>
<Style TargetType="Image" x:Key="ImageStyle">
<Setter Property="Source" Value="{svg2Xaml:SvgImage VideoControllerTester;component/Resources/Start.svg}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding MultiTrigger}" Value="stop">
<Setter Property="Source" Value="{svg2Xaml:SvgImage VideoControllerTester;component/Resources/Stop.svg}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
and apply it to an Image control:
<Image Style="{StaticResource ImageStyle}"/>
Then change the property value somewhere in the MainWindow's code behind by directly accessing the view model instance like this:
((MainViewModel)DataContext).MultiTrigger = "stop";
I have a couple of grids in a user control. I want to hide the context menu in a grid based on a property in the DataContext. I have this code:
<Style TargetType="{x:Type ContextMenu}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsLockedNorthGrid}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
I know the IsLockedNorthGrid property works since I'm using it somewhere else in xaml. What am I missing?
Thanks
If you are using Style.Triggers to change the Visibility, be sure you are not setting Visibility to Context Menu inline. Since, inline property has higher priority over the style.
Check if the Visibility is set to a specific value (setting it in a specific element and triggering it won´t work). Also try
<Style TargetType="{x:Type ContextMenu}">
<Setter Property="Visibility" Value="Visible" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsLockedNorthGrid}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
I was able to solve my problem like so:
<XamDataGrid ContextMenuOpeninig="OnContextMenuOpening">
<XamDataGrid.Resources>
<ContextMenu x:Key="GridContextMenu">...</ContextMenu> </XamDataGrid.Resources>
<XamDataGrid>
Code behind:
private void OnContextMenuOpening(object sender, ContextMenuEventArgs e)
{
var logViewModel = (LogViewModelBase)DataContext;
var grid = sender as XamDataGrid;
var menu = grid.Resources["GridContextMenu"] as ContextMenu;
menu.Visibility = !logViewModel.IsLockedNorthGrid ? Visibility.Hidden : Visibility.Visible;
}
Not that pretty but it works.
<ToggleButton Command="{Binding Path=Command}" Content="{Binding Path=DisplayName}" Template="{Utilities:BindableResource {Binding Path=TemplateResource}}">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Template" Value="{Utilities:BindableResource {Binding Path=SelectedTemplateResource}}" />
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
The first bindable resource works for "TemplateResource" on the template property of togglebutton however "SelectedTemplateResource" does not work within the tiggers setter. This code is within a resourcedictionary where the actual resource is within a themed resourcedictionary.
I get an error saying key is null for xamlparseexception for the setter value. I've been stairing at this for hours but cannot figure out why it doesn't work... If I take out the style and replace the first binding with the second resource it does display proper however the binding within the style will not work.
Does anybody have any idea why?
EDIT
I just tried this but no luck.
<ToggleButton Command="{Binding Path=Command}" Content="{Binding Path=DisplayName}">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Style.Triggers>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Template" Value="{Utilities:BindableResource {Binding Path=TemplateResource}}" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Template" Value="{Utilities:BindableResource {Binding Path=SelectedTemplateResource}}" />
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
After finding out this really is not possible to do in pure xaml I brought out the c# and create a custom control... this is very basic and can be improved on and I will have change a bit of it but ultimately a custom control solves the issue so that you can hit the click event from within the resource dictionary and change the template on the fly.
public class TabButton : Button
{
public static readonly DependencyProperty SelectedTemplateProperty =
DependencyProperty.Register("SelectedTemplate", typeof(ControlTemplate), typeof(TabButton));
public ControlTemplate SelectedTemplate
{
get { return base.GetValue(SelectedTemplateProperty) as ControlTemplate; }
set { base.SetValue(SelectedTemplateProperty, value); }
}
public TabButton()
{
this.Click += new RoutedEventHandler(TabButton_Click);
}
~TabButton()
{
}
public void TabButton_Click(object sender, RoutedEventArgs e)
{
ControlTemplate template = (ControlTemplate)this.FindResource("Environmental Template Selected");
(sender as TabButton).Template = template;
}
}
Cheers.
would like to know how to highlight a gridview row if some object value is greater than another ? (Consider the gridview to be bind to an observablecollection)
Thanks.
Put a property on the class of the items in your observable collection that will get set to true/false based on the comparison you need. Then you should be able to bind to this property in a DataTrigger for a Style on the DataGrid's ItemContainerStyle.
Try this...
<DataGrid ItemsSource="{Binding YourObservableCollection}" >
<DataGrid.ItemContainerStyle>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding YourShouldHighlightProperty}" Value="True">
<Setter Property="Control.Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.ItemContainerStyle>
</DataGrid>
You will need to implement a converter to do a > comparison but this shows a highlight
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsParent}" Value="True">
<Setter Property="Background" Value="Gainsboro" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
Use the following code.It wored for me.
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
try
{
foreach (loadtodatagrid item in gridview.ItemsSource)
{
var row = gridview.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
if (item.item1== item.item2)
{
row.Background = new SolidColorBrush(Colors.Yellow);
}
else
{
// row.Background = new SolidColorBrush(Colors.Green);
}
}
}
catch(Exception ep)
{
//do nothing....
}
LoadingRow="DataGrid_LoadingRow" to datagrid in WPF
What is wrong with this code ? It throws the Exception: "View cannot be shared by more than one ListView"
<ListView
ItemsSource="{Binding}"
SelectionMode="Extended">
<ListView.Style>
<Style TargetType="ListView">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyControl, Path=IsCompany}" Value="True">
<Setter Property="View" Value="{StaticResource GridViewCompanies}" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=MyControl, Path=IsCompany}" Value="False">
<Setter Property="View" Value="{StaticResource GridViewPeople}" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=MyControl, Path=IsCompany}" Value="{x:Null}">
<Setter Property="View" Value="{StaticResource GridViewBoth}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.Style>
</ListView>
public bool? IsCompany
{
get { return (bool?)GetValue(IsCompanyProperty); }
set { SetValue(IsCompanyProperty, value); }
}
public static readonly DependencyProperty IsCompanyProperty =
DependencyProperty.Register("IsCompany", typeof(bool?), typeof(MyControl), new UIPropertyMetadata(null));
EDITED:
I've tried to set the View in code behind and it works. What's the problem with XAML then?
if() ..
MyListView.View = Resources["GridViewCompanies"] as GridView;
The error is because the GridView is being applied to more than one ListView. Is your ListView Style being applied to more than one control? I took a look in Reflector, and it looks like this scenario should work for a single control.
What exactly are you trying to accomplish? I imagine you just want to show different columns. Maybe you can create an attached behavior to generate columns for the GridView depending on the value of the property you are binding to.