Bind Control depending bool - c#

I have to accomplish the following task and don't know how to do it at all.
I am using a WizardControl (XCeed Wpf Toolkit) and want to skip one page, depending on a certain state in the ViewModel (let's say a bool-variable).
To achieve this I have to bind a different Control (a WizardPage) to the previous WizardPage's NextPage DependencyProperty according to this bool-variable. I think this can be done somehow with DataTrigger, but I am not so experienced in this topic. Can someone please help me?
The minimal code sample ist:
<xctk:Wizard>
<xctkWizardPage x:Name="Page1" NextPage="Page2"/>
<xctkWizardPage x:Name="Page2"/>
<xctkWizardPage x:Name="Page3"/>
</xctkWizardPage>
</xctk:Wizard>
public bool Property { get; set; }
What I want to do is make the "NextPage" of "Page1" dependant on the "Property", e.g. have "Page2" as NextPage if Property == true, otherwise go to "Page3".
Thank you very much for your help!
Jan

Maybe something like:
<Style TargetType="xctWizardPage">
<Style.Triggers>
<DataTrigger Property="YourProperty" Value="True">
<Setter Property="NextPage" Value="Page2" />
</DataTrigger>
<DataTrigger Property="YourProperty" Value="False">
<Setter Property="NextPage" Value="Page3" />
</DataTrigger>
</Style.Triggers>
</Style>
Try something like this.

This solution worked for me:
<xctk:Wizard>
<xctk:WizardPage x:Name="Page1">
<Style TargetType="xctk:WizardPage">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Property}" Value="True">
<Setter Property="NextPage" Value="{Binding ElementName=Page2}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Property}" Value="False">
<Setter Property="NextPage" Value="{Binding ElementName=Page3}" />
</DataTrigger>
</Style.Triggers>
</Style>
</xctk:WizardPage>
<xctk:WizardPage x:Name="Page2"/>
<xctk:WizardPage x:Name="Page3"/>
</xctk:Wizard>
Thanks for all replies.

Related

WPF After a data trigger is in use (sealed), it cannot be modified

I have no idea what is wrong, and how I am supposed to solve the error. Strangely enough, the data triggers work fine.
<Style TargetType="Border">
<Setter Property="Background" Value="{StaticResource HeaderBackgroundBrush}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="{StaticResource ErrorState}">
<Setter Property="Background" Value="OrangeRed"/>
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="{StaticResource ProductionState}">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
ProductionState and Error states are enum values referenced in XAML:
<machineControl:MachineControllerState x:Key="ErrorState">Error</machineControl:MachineControllerState>
<machineControl:MachineControllerState x:Key="ProductionState">Production</machineControl:MachineControllerState>
The State binding references to the view model which is "a normal" property which supports IPropertyChanged (from mvvm lights lib)
public MachineControllerState State
{
get => state;
set { Set(() => State, ref state, value); }
}
visual studio shows the error in error list:
After a data trigger is in use (sealed), it cannot be modified
Update: to be clear, the solution compiles fine, and runtime everything works as expected.
You can refer your enum directly from your codebehind. Something like:
{x:Static namespacename:EnumName.EnumValue}
Reference:
How can I use enum types in XAML?

WPF Binding DataTrigger To A Different Property

I have a Teleric RadGrid View like this that's bound to a property called Load:
<telerik:RadGridView x:Name="myRadGridView"
ItemsSource="{Binding Load}">
I have a DataTrigger for that same grid that I want to bind to a property called checkColor which is in the same class as the RadGridView's Load property. I think this isn't working because both properties are in the same class? What would be the correct syntax?
<telerik:RadGridView.Resources>
<Style TargetType="telerik:GridViewRow">
<Style.Triggers>
<DataTrigger Binding="{Binding checkColor}" Value="true">
<DataTrigger.Setters>
<Setter Property="Background" Value="Blue" />
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
</telerik:RadGridView.Resources>
P.s. revisited this and finally found the real solution.
1) Added an item to the Window pointing at the class I wanted to reference.
xmlns:local="clr-namespace:MyClass"
2) Created a static resource to it on a parent component:
<DockPanel.Resources>
<local:Changes x:Key="MyViewModel"/>
</DockPanel.Resources>
3) Set my setter binding to point at it:
<Setter Property="Background" Value="{Binding Source={StaticResource MyViewModel}, Path=checkColor}"/>
Old "Solution":
Ended up solving the problem by putting the data the checkColor function was going to return into a new column in the DataTable that the RadGridView is bound to. Since columns are part of the RadGridView's binding scope I could bind it to the DataTrigger like this:
<telerik:RadGridView.Resources>
<Style TargetType="telerik:GridViewRow">
<Style.Triggers>
<DataTrigger Binding="{Binding checkColorDataColumn}" Value="1">
<DataTrigger.Setters>
<Setter Property="Background" Value="Blue" />
</DataTrigger.Setters>
</Style.Triggers>
</Style>
</telerik:RadGridView.Resources>

How to apply style to treeviewitem if the datacontext is an object of Type T

Is there a way to do this, or do I have to create an IsSelectedProperty on the ViewModel and Bind to that instead?
I would like to be able to do something like Source={Binding RelativeAncestor ListViewItem}
but instead there is only this property sourcename which I can use to set triggers based off of the items in the datatemplate if I name them using x:Name
<HierarchicalDataTemplate.Triggers>
<Trigger SourceName="" Property="IsSelected" Value="True">
<Setter TargetName="bdr" Property="Foreground" Value="White"/>
<Setter TargetName="bdr" Property="Foreground" Value="Red"/>
</Trigger>
</HierarchicalDataTemplate.Triggers>
Looking back, I realized that yesterday I made a very confusing post. Also, considering how hard I find it to even interpret people's comments to my questions, I should probably give more detail.
I know that in wpf you can set triggers based on the control by setting a style for the target control type.
In addition you also have data triggers that can trigger off of properties in the datacontext. What I was hoping was that there was a way to use triggers, or datatriggers to set a property when the datacontext is an object of a specific type.
is this possible? If not, I will just accept the provided answer as applicable in my situation, but since this will require me to add a property to my viewmodel, it seemed reasonable to check if there was a way to just check item type rather than having to check the actual property.
I would suggest that you bind the IsSelected property in your view model, but that's just me.
I'm not sure how complex your HierarchicalDataTemplate is, or if some items need to have their Foreground changed, and some don't; but I'm going to assume you want to update the item that is selected throughout the entire TreeView (if that's what this is for).
If that is the case, just add DataTriggers to the Style of the TreeViewItem:
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected"
Value="{Binding IsSelected, Mode=TwoWay}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected}" Value="True">
<Setter Property="Foreground"
Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding IsSelected}"
Value="False">
<Setter Property="Foreground"
Value="Black" />
</DataTrigger>
</Style.Triggers>
</Style>
Note that you don't need to bind the IsSelected parameter if you don't want to, its just there because it's in my code.

How to set different background according to one property?

I have ListView which ItemSource bindend to ObservableCollection<Period> where Period is
public class Period : INotifyPropertyChanged
{
//some stuff
//
public Status PeriodStatus
{
get;
set;
}
#region PropertyChangedEventHandler members
public void SendPropertyChanged(string name)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
public enum Status
{
None,
Added,
Deleted,
Edited
}
And i want set background of each ListViewItem in this order : added-green/deleted-red/edited-yellow/none-default . Found at here many solutions, but didn't provide my solution. If there is exist question , please comment and i will close this
[EDIT]
I wanted to use DataTemplate in this way: create template which create binding with Background property and Status in Period which uses converter. But didn't know how to keep rest of design
Please use DataTriggers for the Background property in the Style of ListViewItem (example in this question: you don't need the converter, use the enum values instead of integer values).
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Added">
<Setter Property="Background" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Deleted">
<Setter Property="Background" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Edited">
<Setter Property="Background" Value="Yellow" />
</DataTrigger>
<Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
</ListView>
The simplest one is to use Triggers in your ListView.ItemContainerStyle.
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Added">
<Setter Property="Background" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Deleted">
<Setter Property="Background" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=PeriodStatus}" Value="Edited">
<Setter Property="Background" Value="Yellow" />
</DataTrigger>
<Style.Triggers>
<Style>
<ListView.ItemContainerStyle>
This is a sample code: you might need to add a namespace with your enum to use it in XAML.
I find it more easy to have a dedicated PeriodStatusColor readonly property, less 'pure xaml', yes, but less code, and all code in the same place. So this property just returns color for current PeriodStatus. On PeriodStatus change, raise also a PeriodStatusColor PropertyChanged. Use static frozen color, and maybe use a PeriodStatus --> Color static Dictionnary to have clean code.

Source of an Image linked to a Boolean

I want to set the Source of an Image depending of the value of a Boolean.
Here is the code I have :
<Image DockPanel.Dock="Left">
<Image.Source>
[...]
</Image.Source>
</Image>
And in [...] I can access to a Boolean (Path="Item2" - I got a Tuple) and I want to set the value of my Source depending of the value of the Boolean.
I Absolutely got no idea how to do it...
I googled it and found some tips about Setters but I didnt managed to get it work
Any help will be very appreciated !
You could use a Style and DataTriggers:
<Image>
<Image.Style>
<Style TargetType="Image">
<Style.Triggers>
<DataTrigger Binding="{Binding ThatBool}" Value="true">
<Setter Property="Source" Value="Path to image"/>
</DataTrigger>
<DataTrigger Binding="{Binding ThatBool}" Value="false">
<Setter Property="Source" Value="Path to another image"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
(You should be familiar with data binding)
You need a DataTrigger... which by the way requires a Style. Check this link.

Categories

Resources