Xamarin Label IsVisible Propety isn't changing - c#

I have a PopUp Page and in this Popup page i have a Label with IsVisible Property bound to my ViewModel. However the site is not refreshing when the IsVisible Property is changing. Whats wrong with my code?
My Xaml
<StackLayout>
<Label Text="Name der Einkaufsliste" />
<Entry x:Name="entryList" FontSize="20"
Placeholder="z.B. Lidl" />
<Label Text="Liste schon Vorhanden!" TextColor="Red" IsVisible="{Binding IsVisible, Mode=TwoWay}"/>
</StackLayout>
My xaml.cs
PopupViewModel vm = new PopupViewModel();
public PopupViewListeHinzufügen()
{
InitializeComponent();
BindingContext = vm;
}
private void Button_Clicked(object sender, EventArgs e)
{
MasterPage master = new MasterPage();
master.addList(entryList.Text);
}
public void ListeVorhandenMeldung()
{
vm.setLabelVisible();
}
My viewmodel:
public class PopupViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public PopupViewModel()
{
IsVisible = false;
}
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void setLabelVisible()
{
IsVisible = true;
}
private bool isVisible;
public bool IsVisible
{
get
{
return isVisible;
}
set
{
isVisible = value;
OnPropertyChanged("IsVisible");
}
}
}
Thanks for any help!

Related

Change page by button click

In my xaml file i have
<Button
Command="{Binding LoginCommand}"
</Button>
<Frame
Grid.Column="1"
NavigationUIVisibility="Hidden"
Content="{Binding CurrentPage, UpdateSourceTrigger=PropertyChanged}">
</Frame>
My viewmodel class:
private readonly LoginPageView _loginPage; // Page i want to use
private readonly StartPageView _startPage;
private Page _currentPage;
public StartWindowViewModel(LoginPageView loginPage, StartPageView startPage)
{
_loginPage = loginPage;
_startPage = startPage;
CurrentPage = _startPage;
LoginCommand = new RelayCommand(OpenLoginPage, _ => true);
}
public event PropertyChangedEventHandler PropertyChanged;
public Page CurrentPage // I'm binding my frame to this property
{
get => _currentPage;
set
{
_currentPage = value;
OnPropertyChanged();
}
}
public RelayCommand LoginCommand { get; set; }
private void OpenLoginPage(object parameter)
{
CurrentPage = _loginPage;
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
I want to change page displayed in frame by clicking a button. I'm not sure if i should user Frame for this, but it seems okay. The problem that i'm not updating page,
My solution not seems to be best possible:
<Frame
Grid.Column="1"
NavigationUIVisibility="Hidden"
Source="{Binding PageName}">
</Frame>
public string PageName
{
get => _pageName + ".xaml";
set
{
_pageName = value;
OnPropertyChanged();
}
}
I don't actualy like this way of solving, so i would be happy if somebody would give a better approach.

Binding xaml property visibility to ViewModel, control with button

I have a StackLayout property in xmal like shown below:
<StackLayout x:Name="_infoView"
Margin="0,10,0,10"
BackgroundColor="Black"
IsVisible="{Binding State}"/>
and a binding bool variable in ViewModel
private Boolean _state = true;
public Boolean State
{
get { return _state; }
set { }
}
I have a button in my xmal and would like to control the visibility of my StackLayout, So I did something like this:
<Button x:Name="CloseButton"
Grid.Row="0"
Grid.Column="3"
Command="{Binding CloseWindowCommand}"/>
and in ViewModel
CloseWindowCommand = new Command(CloseWindowTapped, CanCloseWindowTapped);
public ICommand CloseWindowCommand { get; set; }
public void CloseWindowTapped()
{
State = false;
}
public bool CanCloseWindowTapped()
{
return true;
}
I'd assume, by tap on the CloseButton, my StackLayout will gone... but it is not working
ViewModel should implement INotifyPropertyChanged interface for informing View about changes.
public class ViewModel : INotifyPropertyChanged
{
// Implementing INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName);
}
// In the setter of property raise event to inform view about changes
private Boolean _state = true;
public Boolean State
{
get
{
return _state;
}
set
{
_state = value;
RaisePropertyChanged();
}
}
}

Binding a string variable to a label

I am trying to move from WinForms to WPF, and am stuck on binding.
I have a label:
<Label Name="labelState" Content="{Binding state}" HorizontalAlignment="Right" Margin="10,10,10,10" FontSize="12" />
In the cs of the same userControl (named FormInput), I have :
public string state { get; set; }
public FormInput()
{
state = "ok";
InitializeComponent();
}
Why doesn't this work?
Thank you.
When you are binding something in WPF you need to use INotifyPropertyChanged
Implement a class follows,
class TestObject : INotifyPropertyChanged
{
private string _state;
public string State
{
get
{
return _state;
}
set
{
if (_state == value) return;
_state = value;
OnPropertyChanged("State");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
and in your FormInput
public FormInput()
{
InitializeComponent();
TestObject t = new TestObject();
labelState.DataContext = t;
t.State = "ok";
}
and XAML as follows,
<Label Name="labelState" Content="{Binding State}" HorizontalAlignment="Right" >

Notify Property Changed not working

this my xml code
<TextBlock Grid.Column="0" Tag="{Binding id,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
this is my model
public string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; RaisePropertyChanged("Name"); }
}
when i set value to these two propertie ie. to id and Name
but its not notifying to Name ...
Simple Databinding Example with Updates. You can use this as a reference to get you started :)
public partial class MainWindow : Window, INotifyPropertyChanged
{
// implement the INotify
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private string _mytext;
public String MyText
{
get { return _mytext; }
set { _mytext = value; NotifyPropertyChanged("MyText"); }
}
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = this; // set the datacontext to itself :)
MyText = "Change Me";
}
}
<TextBlock Text="{Binding MyText}" Foreground="White" Background="Black"></TextBlock>

WPF Binding to a property in second ViewModel

How do I get the text bound to txtMessage from the second view model? When I had only one view model, the text was working fine. It does not work anymore when I moved the actual download code to second view model. Am I missing something? Any help appreciated.
Xaml:
<DockPanel DockPanel.Dock="Top">
<TextBlock x:Name="txtMessage" DockPanel.Dock="Top" Margin="5" Text="{Binding viewModel1.Message}" />
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="5,5">
<ProgressBar Width="300" Visibility="{Binding IsDownloading, Converter={converter:VisibilityConverter}}" IsIndeterminate="True" />
<Button Content="Cancel" />
</StackPanel>
</DockPanel>
<Button Content="Download" Width="120" Margin="0,0,5,0" Name="btnSubmit" Click="btnSubmit_Click" />
CodeBehind:
public partial class DownloadWindow: Window
{
DownloadWindowViewModel viewModel = new DownloadWindowViewModel();
public DownloadWindow()
{
InitializeComponent();
this.DataContext = viewModel;
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
viewModel.IsDownloading = true;
viewModel.Download();
}
}
viewModel:
public class DownloadWindowViewModel: INotifyPropertyChanged
{
Thread downloadThread;
public DownloadViewModel viewModel1;
public DownloadWindowViewModel()
{
viewModel1 = new DownloadViewModel();
}
private bool _isDownloading; = false;
public bool IsDownloading
{
get
{
return _isDownloading;
}
set
{
_isDownloading; = value;
OnPropertyChanged("IsDownloading");
}
}
public void Download()
{
viewModel1.Download();
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
viewModel1:
public class DownloadViewModel: INotifyPropertyChanged
{
Thread _thread;
public void Download()
{
ThreadStart threadStart = delegate()
{
StartDownload();
};
_thread = new Thread(threadStart);
_thread.IsBackground = true;
_thread.Start();
}
private void StartDownload()
{
for (int i = 10; i < 1500; i++)
{
Thread.Sleep(5000);
Message = "Downloading " + i.ToString();
}
}
private string _message = "";
public string Message
{
get
{
return _message;
}
set
{
if (_message != value)
{
_message = value;
OnPropertyChanged("Message");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Your viewModel1 has to be a property, and it's a field at the moment. Change it to:
public DownloadViewModel viewModel1 { get; set; }
Explanation why such restriction exists, can be found here (primarily due to notification/verifications mechanisms simply not working for fields):
Why does WPF support binding to properties of an object, but not fields?

Categories