Usher
Usher

Reputation: 2146

Label is not hidden in WPF binding

I have a new issue with the Label button now. The code below binds the view to the view model:

<Label Name="isImageValid"  Content="Image not Created" Margin="0,7,1,0" Style="{StaticResource LabelField}"
                Grid.ColumnSpan="2" Grid.Row="15" Width="119" Height="28" Grid.RowSpan="2"
                Grid.Column="1" IsEnabled="True" 
                Visibility="{Binding isImageValid}" />

And the following is the code from my ViewModel:

 private System.Windows.Visibility _isImageValid;
 public System.Windows.Visibility isImageValid
        {

            get
            {

                return _isImageValid;
            }
            set
            {


                _isImageValid = value;
               this.RaisePropertyChanged(() => this.isImageValid);

            }
        }
  private void OnImageResizeCompleted(bool isSuccessful)
    {

        if (isSuccessful)
        {

            this.SelectedStory.KeyframeImages = true;
            isImageValid = System.Windows.Visibility.Visible;
        }
        else
        {
            this.SelectedStory.KeyframeImages = false;

        }
    }

The Label is meant to stay hidden until "OnImageResizeCompleted" is called, but for some reason the image is visible all the time. What would I need to change to hide it, please?

Upvotes: 1

Views: 4385

Answers (2)

slugster
slugster

Reputation: 49974

Your issue is not with the actual binding mode, a label doesn't need two way binding because it doesn't usually set its source.

As @blindmeis suggested, you should use a converter instead of returning a Visibility value directly from the viewmodel, there is one built into the framework you can use. You should also ensure your datacontext is set correctly, if it isn't then the label won't be able to bind to the specified property. Do you have other items on the same window that are binding correctly to the viewmodel? You should also check your output window for binding errors - they will be mentioned there. Finally you should also check that your property is notifying correctly - that is impossible to tell from the code you provided.

Your control/window should look something like this

<UserControl    x:Class="..."
                x:Name="MyControl"

                xmlns:sysControls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"

                >   
    <UserControl.Resources> 
        <sysControls:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </UserControl.Resources>

    <Grid>
        <Label  Visibility="{Binding IsImageValid, Converter={StaticResource BooleanToVisibilityConverter}}" />
    </Grid>
</UserControl>

and the C#:

public class MyViewModel : INotifyPropertyChanged
{

    public bool IsImageValid 
    {
        get { return _isImageValid; }
        set 
        {
            _isImageValid = value;
            OnPropertyChanged("IsImageValid");
        }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private bool _isImageValid;
}

Upvotes: 3

blindmeis
blindmeis

Reputation: 22445

try setting the Binding mode to twoway

<Label  Visibility="{Binding isImageValid, Mode=TwoWay}" />

nevertheless i would not use the System.Windows namespace in a viewmodel. create a bool property and use a booltovisibility converter in your binding.

Upvotes: 0

Related Questions