Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVM Binding Issue - Another Noob-ish Issue
    primarykey
    data
    text
    <p>So here I am again, asking a <a href="https://stackoverflow.com/questions/14692486/wpf-databinding-issues-possible-noob-problems">very similar question to yesterday</a>. I re-factored my project in order to better follow the MVVM pattern. Now my binding is no longer working as it was yesterday. I am trying to bind the visibility of a dock panel to a button. Here is some of my code:</p> <p>ViewModel:</p> <pre><code>public class SelectWaferButtonViewModel : INotifyPropertyChanged { private bool isClicked; public SelectWaferButtonViewModel() { isClicked = false; } public bool IsControlVisible { get { return isClicked; } set { isClicked = value; OnPropertyChanged("IsControlVisible"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnButtonClick() { if (isClicked) { IsControlVisible = false; } else { IsControlVisible = true; } } protected virtual void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } } </code></pre> <p>XAML:</p> <pre><code>&lt;Window.Resources&gt; &lt;local:BoolToVisibilityConverter x:Key="BoolToVisConverter"/&gt; &lt;local:SelectWaferButtonViewModel x:Key="SelectWaferButton" /&gt; &lt;local:WaferTrackerWindowViewModel x:Key="WindowViewModel" /&gt; &lt;/Window.Resources&gt; &lt;DockPanel Name="tvwDockPanel" DataContext="{StaticResource SelectWaferButton}" Width="225" Visibility="{Binding IsControlVisible, Mode=TwoWay, FallbackValue=Collapsed, Converter={StaticResource BoolToVisConverter}}" DockPanel.Dock="Left"&gt; &lt;/DockPanel&gt; </code></pre> <p>My <code>BoolToVisConverter</code>:</p> <pre><code>public class BoolToVisibilityConverter : IValueConverter { public BoolToVisibilityConverter() { } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { bool bValue = (bool) value; if (bValue) { return Visibility.Visible; } else { return Visibility.Collapsed; } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { Visibility visibility = (Visibility) value; if (visibility == Visibility.Visible) { return true; } else { return false; } } } </code></pre> <p>I apologize for a question that is similar to yesterday, but I am struggling with this MVVM stuff since I am quite new to WPF. Any help will be much appreciated.</p> <ul> <li>Thanks in advanced,</li> </ul> <p><strong><em>EDIT:</em></strong> <strong><em>Here is some extra code snippets for further reference:</em></strong></p> <pre><code>public class WaferTrackerWindowViewModel :INotifyPropertyChanged { private SelectWaferButtonViewModel btnSelectWaferViewModel; public event PropertyChangedEventHandler PropertyChanged; private DelegateCommand exitCommand; private DelegateCommand expandPanelCommand; private DelegateCommand selectWaferCommand; public WaferTrackerWindowViewModel() { this.InstantiateObjects(); initThread.RunWorkerAsync(); } public string SelectedWafer { get { return selectedWafer; } set { selectedWafer = value; } } public ICommand ExitCommand { get { if (exitCommand == null) { exitCommand = new DelegateCommand(Exit); } return exitCommand; } } public ICommand ExpandPanelCommand { get { if (expandPanelCommand == null) { expandPanelCommand = new DelegateCommand(ExpandPanel); } return expandPanelCommand; } } public ICommand SelectWaferCommand { get { if (selectWaferCommand == null) { selectWaferCommand = new DelegateCommand(SelectWafer); } return selectWaferCommand; } } private void InstantiateObjects() { btnSelectWaferViewModel = new SelectWaferButtonViewModel(); initThread = new BackgroundWorker(); } private void ExpandPanel() { btnSelectWaferViewModel.OnButtonClick(); } private void SelectWafer() { //Does Nothing Yet } private void Exit() { Application.Current.Shutdown(); } private void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } private void InitThread_DoWork(object sender, DoWorkEventArgs e) { TreeViewPresenter tvwPresenter = new TreeViewPresenter(); tvwPresenter.WaferList = DataLibrary.GetWaferList(); } private void InitThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { tvwPresenter.TreeView.DataContext = tvwPresenter.ProcessesAndWafers; tvwPresenter.WaferListCache = tvwPresenter.ProcessesAndWafers; tvwPresenter.ProcessArray = tvwPresenter.WaferListCache.ToArray(); } } </code></pre> <p><strong><em>When the "expand panel" button gets clicked, it calls the <code>ExpandPanel</code> command, which routes the execution to the method "private void ExpandPanel()" in this same class. Then, in the <code>ExpandPanel()</code> method, it calls the <code>OnButtonClick()</code> method on the <code>btnSelectWaferViewModel</code> object, which will change the <code>IsControlVisible</code> property. This change should then be reflected onto the bound dock panel, but this is not happening</em></strong> </p> <p>Kyle</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload