locked
How to access singleton class in xaml. RRS feed

  • Question

  • Hi all,

    I have a singleton class, as following,

    public calss Mysingleton
    {
    private static Mysingleton _instance = null;
    public static Mysingleton Instance
    {
    get
    {
    if(_instance == null)
    {
    _instance = new Mysingleton();
    }
    return _instance;
    }
    }
        protected Mysingleton()
    {};
    private bool _isConnect = false;
    public bool IsConnect
    {
    get
    {
    return this._isConnect;
    }
    set
    {
    this._isConnect = value;
    }
    }
    }

    Then I have a button in xaml, and I want to binding the "IsEnabled" to "IsConnect".

    For example:

    <Button IsEnabled="{Binding Path=IsConnect,source={????}}"/>

    How can I implement it?

    Thanks!


    Tuesday, December 16, 2014 6:36 AM

Answers

  • You could simply set the Source to the Instance property of the Mysingleton class using an x:Static markup extension:


        <Button xmlns:local="clr-namespace:WpfApplication1"  IsEnabled="{Binding Path=IsConnect, Source={x:Static local:Mysingleton.Instance}}"/>

    In the example above, "WpfApplication1" is the namespace in which the Mysingleton class is defined. Replace this with the name of your namespace.

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by Shirley-cheng Friday, December 19, 2014 7:29 AM
    Tuesday, December 16, 2014 10:08 AM

All replies

  • Hi,

    You can try :

    <Button Name="thisButton" IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=DataContext.IsConnect}"  ></Button>

    Provided that you have below:

    this.thisButton.DataContext = MySingleton.Instance;


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by giovhan Tuesday, December 16, 2014 7:38 AM
    Tuesday, December 16, 2014 7:38 AM
  • If you want a singleton class used in xaml then I suggest you use either a markup extension or a converter.

    By default there is only one instance of either created.

    So if you use a markup extension in 27 places, it's one instance.

    It can also get at properties of the control it's wired up to.

    .

    BUT

    .

    What you're doing there seems to be just sharing the one property across your application.

    I suggest you just use one variable instead of a singleton.

    You can then just do:

    <Button IsEnabled="{DynamicResource IsConnect}"/>
     

    "Declare" the variable in app.xaml or a resource dictionary merged by app.xaml:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
                        xmlns:sys="clr-namespace:System;assembly=mscorlib" 
                        >
        <sys:Boolean x:Key="IsConnect">False</sys:Boolean> 
    </ResourceDictionary>

    Note the namespace xmlns

    You can get at that in code as well.

    bool isConnected = (Boolean)Application.Current.Resources["IsConnected"];

    When it changes in one place it's a dependency property so everything bound to it is notified and will respond.


    Hope that helps
    Please don't forget to upvote posts which you like and mark those which answer your question.

    Tuesday, December 16, 2014 8:33 AM
  • You could simply set the Source to the Instance property of the Mysingleton class using an x:Static markup extension:


        <Button xmlns:local="clr-namespace:WpfApplication1"  IsEnabled="{Binding Path=IsConnect, Source={x:Static local:Mysingleton.Instance}}"/>

    In the example above, "WpfApplication1" is the namespace in which the Mysingleton class is defined. Replace this with the name of your namespace.

    Please remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by Shirley-cheng Friday, December 19, 2014 7:29 AM
    Tuesday, December 16, 2014 10:08 AM
  • Hi Magnus,

    Thank you a lot!

    Friday, December 19, 2014 7:29 AM
  • Hi Andy ONeill,

    Thanks.

    But your method is not what I want to.

    Friday, December 19, 2014 7:30 AM
  • Hi giovhan,

    Thanks any way!

    Friday, December 19, 2014 7:31 AM