Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVVM Binding OnLabel isn't working #1867

Closed
TravyDale opened this issue Apr 6, 2015 · 5 comments
Closed

MVVM Binding OnLabel isn't working #1867

TravyDale opened this issue Apr 6, 2015 · 5 comments
Assignees
Labels
Milestone

Comments

@TravyDale
Copy link

So I am using MVVM Light and I am binding my OnLabel to a string property I have in my ViewModel. This works only when I have the toggle NOT checked. Once I check it, the OnLabel goes to the correct string. If I have the toggle initially checked, the OnLabel is always 'On' and not whatever my string is inside of my ViewModel. If I uncheck the toggle, then check it, the correct string shows. Any thoughts?

@killnine
Copy link

killnine commented Apr 8, 2015

Can you provide both the XAML and the ViewModel wire-up?

@TravyDale
Copy link
Author

ViewModel + Model:

public class DemoViewModel : ViewModelBase
    {
        #region -- Fields --

        private Model model = null;

        #endregion

        #region -- Properties --

        public Model Model
        {
            get { return this.model; }
            set
            {
                this.model = value;
                RaisePropertyChanged("Model");
            }
        }

        #endregion

        #region -- Commands --

        public RelayCommand GoCommand { get; set; }

        #endregion

        public DemoViewModel()
        {
            this.GoCommand = new RelayCommand(Initialize);
        }

        protected void Initialize()
        {
            var model = new Model();
            model.Initialize();

            this.Model = model;
        }
    }

    public class Model : ObservableObject
    {
        #region -- Fields --

        private bool isChecked = false;

        private string label = string.Empty;

        #endregion

        #region -- Properties --

        public bool IsChecked
        {
            get { return this.isChecked; }
            set
            {
                this.Label = value ? "1234" : string.Empty;

                this.isChecked = value;
                RaisePropertyChanged("IsChecked");
            }
        }

        public string Label
        {
            get { return this.label; }
            set
            {
                this.label = value;
                RaisePropertyChanged("Label");
            }
        }

        #endregion

        public void Initialize()
        {
            this.Label = "4321";
            this.isChecked = !string.IsNullOrWhiteSpace(this.Label);
        }
    }

View

<Window x:Class="WpfScratch.DemoWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DemoWindow" Height="300" Width="300"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        DataContext="{Binding DemoViewModel, Source={StaticResource Locator}}">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <Controls:ToggleSwitch Width="110" IsChecked="{Binding Model.IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" OnLabel="{Binding Model.Label}" OffLabel="" />
            <Button Content="Go" Command="{Binding GoCommand}" />
        </StackPanel>
    </Grid>
</Window>

The only way I can work around this is for me to replace the control's OnLabel="{Binding}" with Style="{DynamicResource LabelToggleSwitch}" and then add the style in my Window.Resources ---

<Style x:Key="LabelToggleSwitch" TargetType="{x:Type Controls:ToggleSwitch}" BasedOn="{StaticResource {x:Type Controls:ToggleSwitch}}">
                <Setter Property="OnLabel" Value="{Binding Label, UpdateSourceTrigger=PropertyChanged}" />
            </Style>

@killnine
Copy link

killnine commented Apr 9, 2015

I believe the issue you're seeing is because you're calling Initialize() before you assign your model instance to the property Model in your ViewModel (wow, that sounds confusing!).

Basically, your Model creates an INotifyPropertyChange event in the Initialize method, but there's nothing listening for it yet. So the view never gets the message to update to 4321.

Try calling Initialize() after you set Model = model.

@killnine
Copy link

@TravyDale did this resolve your issue? If so, can you mark issue as closed?

@KKauK
Copy link

KKauK commented Apr 28, 2015

@killnine I do have the same issue. Changing my localization only get's reflected in the On/Off labels after toggling the switch. Works with all other controls on my view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants