I'm trying to show/hide buttons on ListBox items by using multiple data triggers. The problem it doesn't work properly.
The changes of the properties are triggering well however the conditions seems not to be met.
This is how I use the triggers. Simply an if/else block.
<DataTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsDownloading}" Value="False"/>
<Condition Binding="{Binding downloaded}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="ButtonDownload" Property="Visibility" Value="1"/>
<Setter TargetName="DownloadProgressPanel" Property="Visibility" Value="1"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsDownloading}" Value="True"/>
<Condition Binding="{Binding downloaded}" Value="False"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="DownloadProgressPanel" Property="Visibility" Value="0"/>
<Setter TargetName="ButtonDownload" Property="Visibility" Value="1"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsDownloading}" Value="False"/>
<Condition Binding="{Binding downloaded}" Value="False"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="DownloadProgressPanel" Property="Visibility" Value="1"/>
<Setter TargetName="ButtonDownload" Property="Visibility" Value="0"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<DataTrigger Binding="{Binding favouriteListType}" Value="8">
<Setter TargetName="URLButtonsPanel" Property="Visibility" Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Related
I have a WPF treeview, with nodes visibility dependent on their "Deleted" binding property:
<Style x:Name="SelectedTreeItem" TargetType="TreeViewItem">
<EventSetter Event="MouseRightButtonDown" Handler="TreeSetup_MouseRightButtonDown"/>
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="Visibility" Value="Visible" />
<Style.Triggers>
<DataTrigger Binding="{Binding Deleted}" Value="True">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
I would like to make add some way to ignore this deleted property value. This would be achieved by a button "Show deleted items".
I do not want to change the "Deleted" value, but rather add a global condition to that forces this binding to ignore untill the button is pressed again.
You can do that via MultiDataTrigger.Conditions. Here is an example for visibility:
<Style>
<Setter Property="Control.Visibility" Value="Visible" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=IsPropA}" Value="false" />
<Condition Binding="{Binding Path=IsPropB}" Value="false" />
</MultiDataTrigger.Conditions>
<Setter Property="Control.Visibility" Value="Hidden" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
I used the other answer in conjuction with a static resource
<Style>
<Setter Property="Control.Visibility" Value="Visible" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=IsPropA}" Value="false" />
<Condition Binding="{Binding Source={StaticResource ClassName},Path=PropertyName}" Value="False"/>
</MultiDataTrigger.Conditions>
<Setter Property="Control.Visibility" Value="Hidden" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
Just had to create the local resource under Window resources:
<Window.Resources>
<local:ClassName x:Key="ClassName"/>
And then the button used to show deleted or not:
private void btnApply_Click(object sender, RoutedEventArgs e)
{
//ApplyGeneralChanges();
var item = this.FindResource("ClassName") as ClassName;
item.DisplayDeleted = !item.DisplayDeleted;
}
I have a MultiDataTrigger. I can bind to a DependencyProperty (DP) of the control and a view model property like this
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="True"/>
<Condition Binding="{Binding PerformTextSearchesInCommentary}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="LightGray"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
Great. But now, I want to use my own custom DP "HasTextMatch", which is defined in a static class. I can do this with a standard trigger like
<Style.Triggers>
<Trigger Property="Helpers:DataGridTextSearch.HasTextMatch" Value="True">
<Setter Property="Background" Value="LightGray"/>
</Trigger>
</Style.Triggers>
But I now want to include another Property. I have tried
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<!--<Condition Property="Helpers:DataGridTextSearch.HasTextMatch" Value="True"/>--> This obviously won't work.
<Condition Binding="{Binding Helpers:DataGridTextSearch.HasTextMatch, RelativeSource={RelativeSource Self}}" Value="True"/>
<Condition Binding="{Binding PerformTextSearchesInCommentary}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="LightGray"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
But this does not work. I have searched but cannot seem to find out how to do this. How can I get the binding to my custom DP?
Thanks for your time.
The DataGridTextSearch.HasTextMatch property is an attached property. Please use the following Condition:
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=(Helpers:DataGridTextSearch.HasTextMatch), RelativeSource={RelativeSource Self}}>
...
Additional information about property path (including attached properties) can be found here: PropertyPath XAML Syntax, MSDN.
I have datagrid with some conditional row. And I want if I select particular row in datagrid, The selected color will change based on my record value.
Suppose I have record of Students with IsDropOut Flag.
And then If I click selected record of student, Selected row will change color according to IsDropOut value. If it is true it will set selected row background to red and if IsDropOut is false it will change selected row background to green.
Maybe with like this
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=DataGridCell.IsSelected}" Value="True" />
<Condition Binding="{Binding Path=IsDropOut}" Value="true" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="red" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
But that's code is not work. So how to do it? Anyone has suggestion?
Finally I solve this.. Thanks for the idea..
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=IsDropOut}" Value="True" />
<Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="White" />
</MultiDataTrigger>
Try this :
<Style TargetType="DataGridCell">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="IsDropOut" Value="true" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Red" />
</MultiTrigger>
</Style.Triggers>
</Style>
I have a DataGrid, where I currently set to ReadOnly when I don't want the user to change anything, then I have a button for the user to click to allow Modify, which then changes IsReadOnly to false
but the problem now is that when the DataGrid.IsReadOnly = true, the scrolling is disabled. How do I make it so that the DataGrid IsReadOnly=true or similar and still be able to scroll the DataGrid?
DataGrid Style:
<DataGrid.Style>
<Style TargetType="{x:Type DataGrid}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ReadOnly}" Value="True"/>
<Condition Binding="{Binding Reordering}" Value="False"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="IsHitTestVisible" Value="False" />
<Setter Property="Foreground" Value="Gray" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ReadOnly}" Value="True"/>
<Condition Binding="{Binding Reordering}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsHitTestVisible" Value="False" />
<Setter Property="IsReadOnly" Value="True" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ReadOnly}" Value="False"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsHitTestVisible" Value="True" />
<Setter Property="IsReadOnly" Value="False" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Style>
Have you tried this:
dataGridView1.ReadOnly = true;
void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if (dataGridView1.ReadOnly == true)
return;
// .. whatever code you have in your handler...
}
Disable DataGridView except the scroll
I have a ListView with AlternationCount set to 2. I have a ListViewItem style that currently sets the background color of the ListViewItem to alternating colors, and I would like to add a third trigger which triggers a ColorAnimation to animate the color between Red and White when a property of the bound ViewModel is true (in this case a property called "Locked").
I came across this post, and tried the xaml at the bottom:
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e7897cbd-71d9-45e6-9b17-0cd5bde5859f
But, the animation doesn't appear to trigger for me until I mouse over or select the item, then select or mouse over a different item. I get the alternating colors from the first two MultiDataTriggers, but the animation doesn't trigger when the item is added to the collection. Here is my XAML:
<Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="White" />
<Style.Triggers>
<MultiDataTrigger >
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Value="0" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ListViewItem.IsSelected)}" Value="False" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Panel.IsMouseOver)}" Value="False" />
<Condition Binding="{Binding Locked}" Value="False" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="DarkGray" />
</MultiDataTrigger>
<MultiDataTrigger >
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Value="1" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ListViewItem.IsSelected)}" Value="False" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Panel.IsMouseOver)}" Value="False" />
<Condition Binding="{Binding Locked}" Value="False" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="SlateGray" />
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ListViewItem.IsSelected)}"
Value="False" />
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Panel.IsMouseOver)}"
Value="False" />
<Condition Binding="{Binding Locked}"
Value="True" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
From="Red" To="White" Duration="0:0:0.2"
AutoReverse="True" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</MultiDataTrigger.EnterActions>
</MultiDataTrigger>
</Style.Triggers>
</Style>
I don't have the specific syntax for you example but you might try putting the triggers directly on the ListView (not in resources) and use ListView.ItemContainerStyle rather than Syle. I could not make the simple code below work in Resouces but when I moved it directly to the ListView it worked. I probably had a syntax error when it was in resources e.g.
<ListView AlternationCount="2"
ItemsSource="{Binding Path=...}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Style.Triggers>
<!-- setting up triggers for alternate background colors -->
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="Gainsboro"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="White"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>