locked
How use dynamic properties in datatriggers RRS feed

  • Question

  • Hi. I have Configuration.cs and MainWindow.xaml. Both of them are in projects root folder.

    Configuration.cs:

    class

     

    Configuration

    {

     

    public static Config MyConfiguration { get; set

    ; }

    }

     

    class

    Config

    {

     

    public bool FirstMenuItemVisibility { get; set

    ; }

     

    public bool SecondMenuItemVisibility { get; set

    ; }

    }

     

     

    MainWindow.xaml has two MenuItems and I want to hide or show them according to the two properties in MyConfiguration.

    Please help me to construct the datatriggers...

     

    Wednesday, November 16, 2011 8:19 AM

Answers

  • Hi,
    Use it as follows:
     <Style x:Key="MenuStyle1" TargetType="{x:Type MenuItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding FirstMenuItemVisibility}" Value="false">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
            <Style x:Key="MenuStyle2" TargetType="{x:Type MenuItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SecondMenuItemVisibility}" Value="false">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
    
    <Grid>
                    <Menu Height="30" VerticalAlignment="Top" IsMainMenu="True" >
                        <MenuItem Header="Hdr1" Style="{StaticResource MenuStyle1}"/>
                        <MenuItem Header="Tools" Style="{StaticResource MenuStyle2}"/>
                    </Menu>
                </Grid>
    
    Assuming that the datacontext exposes these 2 properties (FirstMenuItemVisibility,SecondMenuItemVisibility) as booleans.
    You can customize your code as per yuor requirement.

    Thanks,
    Abhinav
    Please mark as solution if this answer helped you
    • Marked as answer by Sheldon _Xiao Thursday, November 24, 2011 9:44 AM
    Wednesday, November 16, 2011 10:27 AM
  • Hi GaiusBaltar,

    You could double check the DataContext property, if the property is correct, I think your application will work well, additional, if you want to debug why "Binding="{Binding Configuration.MyConfiguration.SecondMenuItemVisibility}" " could not work, you could add a converter to debug this Binding part, check the value, then you will know how to bind it, refer to:

    http://social.msdn.microsoft.com/Forums/sr-Latn-CS/wpf/thread/549eeb7c-8df7-4a6c-a264-91f06ca75293

     

    best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Sheldon _Xiao Thursday, November 24, 2011 9:44 AM
    Thursday, November 17, 2011 7:34 AM

All replies

  • Hi,

    Use this way

    XAML file :

    <Window x:Class="WpfApplication1.Window12"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window12" Height="300" Width="300">
        <Grid>
            <Menu Height="30" VerticalAlignment="Top" IsMainMenu="True">
                <MenuItem Header="File" Visibility="{Binding MyConfiguration.FirstMenuItemVisibility}" />
                <MenuItem Header="Tools" Visibility="{Binding MyConfiguration.SecondMenuItemVisibility}" />
            </Menu>
        </Grid>
    </Window>
    
    


    code behind :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.ComponentModel;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for Window12.xaml
        /// </summary>
        public partial class Window12 : Window, INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            public Window12()
            {
                InitializeComponent();
                this.DataContext = this;
                MyConfiguration.FirstMenuItemVisibility = Visibility.Collapsed;
                MyConfiguration.SecondMenuItemVisibility = Visibility.Visible;
                OnPropertyChanged("MyConfiguration");
            }
    
            private Config _myConfiguration=new Config();
    
            public Config MyConfiguration
            {
                get { return _myConfiguration; }
                set { _myConfiguration = value; OnPropertyChanged("MyConfiguration"); }
            }
    
    
    
        }
    
        public class Config : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            private Visibility _firstMenuItemVisibility;
    
            public Visibility FirstMenuItemVisibility
            {
                get { return _firstMenuItemVisibility; }
                set { _firstMenuItemVisibility = value; OnPropertyChanged("FirstMenuItemVisibility"); }
            }
    
            private Visibility _secondMenuItemVisibility;
    
            public Visibility SecondMenuItemVisibility
            {
                get { return _secondMenuItemVisibility; }
                set
                {
                    _secondMenuItemVisibility = value;
                    OnPropertyChanged("SecondMenuItemVisibility");
                }
            }
        }
    
    
    }
    
    

     

    Thanks,

    Rajnikant

    Wednesday, November 16, 2011 8:56 AM
  • this datatrigger is in code behind. I need to do it in XAML
    • Edited by GaiusBaltar Wednesday, November 16, 2011 9:17 AM typo
    Wednesday, November 16, 2011 9:06 AM
  • Hi,

    You can do it without datatrigger, see i have created properties in Config class of type visibility, i bound it directly to visibility property of menuitem.

    Just copy and paste code and see result.

    Thanks,

    Rajnikant


    Wednesday, November 16, 2011 9:19 AM
  • yes but the thing is that I need to learn how to use datatrigger in XAML. I know this solution already but my assignemnt is to use datatrigger in XAML :)
    Wednesday, November 16, 2011 9:25 AM
  • Hi,
    Use it as follows:
     <Style x:Key="MenuStyle1" TargetType="{x:Type MenuItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding FirstMenuItemVisibility}" Value="false">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
            <Style x:Key="MenuStyle2" TargetType="{x:Type MenuItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SecondMenuItemVisibility}" Value="false">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
    
    <Grid>
                    <Menu Height="30" VerticalAlignment="Top" IsMainMenu="True" >
                        <MenuItem Header="Hdr1" Style="{StaticResource MenuStyle1}"/>
                        <MenuItem Header="Tools" Style="{StaticResource MenuStyle2}"/>
                    </Menu>
                </Grid>
    
    Assuming that the datacontext exposes these 2 properties (FirstMenuItemVisibility,SecondMenuItemVisibility) as booleans.
    You can customize your code as per yuor requirement.

    Thanks,
    Abhinav
    Please mark as solution if this answer helped you
    • Marked as answer by Sheldon _Xiao Thursday, November 24, 2011 9:44 AM
    Wednesday, November 16, 2011 10:27 AM
  • almost, but does not work for me. I need to refer to the Configuration.cs somehow.

     I need to use something like Binding="{Binding Configuration.MyConfiguration.SecondMenuItemVisibility}" - but this does not work

    Wednesday, November 16, 2011 11:35 AM
  • Hi,

    The datacontext has to be set appropriately. You can refer to the code mentioned by Rajni above which will resolve your issue. you will have to bind to MyConfiguration property from that code.

    Please let us know if it doesn't resolve your issue.


    Thanks,
    Abhinav
    Please mark as solution if this answer helped you
    Wednesday, November 16, 2011 12:14 PM
  • Hi GaiusBaltar,

    You could double check the DataContext property, if the property is correct, I think your application will work well, additional, if you want to debug why "Binding="{Binding Configuration.MyConfiguration.SecondMenuItemVisibility}" " could not work, you could add a converter to debug this Binding part, check the value, then you will know how to bind it, refer to:

    http://social.msdn.microsoft.com/Forums/sr-Latn-CS/wpf/thread/549eeb7c-8df7-4a6c-a264-91f06ca75293

     

    best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Sheldon _Xiao Thursday, November 24, 2011 9:44 AM
    Thursday, November 17, 2011 7:34 AM
  • Hi GaiusBaltar,

    I am marking your issue as "Answered", if you have new findings about your issue, please let me know.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 24, 2011 9:44 AM