0

I apologize if the question title isn't really specific, I'm not exactly sure how to condense the problem I'm having down to a few words. But to simplifiy the problem I'm having, here is my issue:

I'm creating a tool using WPF that consists of a TextBox that will contain a path to a directory and a Button that will allow you to Browse to a certain directory. Now, when I select the Browse button, it pops up a dialog, allows the user to select a directory and then I have some methods that will disable some buttons and updates some Brushes on the screen if the path doesn't meet a certain set of criteria. No problems there, got that working.

My problem is the TextBox that this Browse button correlates with. This TextBox is using a binding as such:

In my MainWindow.xaml (Yes, this is the simplified, focused version):

<Window>
    <TextBox Text="{Binding Directory}" TextChanged="Directory_TextChanged" />
    <Button Content="Browse..." Click="Browse_Click"/>
</Window>

In my code MainWindow.xaml.cs file:

public partial class MainWindow: Window
{
  private ViewModel myViewModel;

  public MainWindow()
  {
    myViewModel = new ViewModel();
    this.DataContext = myViewModel;
  }

  private void Browse_Click(object sender, RoutedEventArgs e)
  {
    // Dialog stuff that's working
    viewModel.Directory = dialog.SelectedPath;
  }

  private void InstallDir_TextChanged(object sender, TextChangedEventArgs e)
  {
    ValidatePath(); /* Disables/enables buttons and updates brushes based on validation. Also working */
  }

  private void ValidatePath() {/* */}
}

Like I mentioned earlier, the browse button works fine. I'm trying to figure out however, how I can get this to work if I type a directory alongside it. Because if I type something in the textbox, that would mean that inside of the InstallDir_TextChanged() function I would have to set viewModel.Directory, but since I have the INotifyPropertyChanged attached to this ViewModel, this function would get called recursively.

I tried doing the validation stuff within the viewmodel, but I couldn't figure out how to update the brushes/buttons in MainWindow if I did this. (Still relatively new to C# so I haven't learned the ins and outs yet. This is the first WPF tool I've been making from scratch, so just a disclaimer).

Would anyone have any ideas (or logic) I can approach to try and accomplish this? If there's any further clarification needed, that's not an issue. I don't need an exact definitive answer. Maybe some advice that could point me in the correct direction would definitely suffice. I don't have a problem trying to figure stuff out.

Chris
  • 2,129
  • 6
  • 22
  • 1
    bind to properties on your ViewModel for UI stuff and use ValueConverters to change colors, enable things, etc.. – kenny Feb 22 '18 at 19:06
  • @kenny Yeah, that's what I saying towards the end here. I originally had the ValidatePath() in the ViewModel, but I couldn't figure out how to modify the buttons/brushes (or call a function to do so) in `MainWindow` from the ViewModel. Do you know how I could do that? If so, that would work. -- Edit: Just saw your edit. Okay, I'll try and see if I can find some examples of that and give it a shot. – Chris Feb 22 '18 at 19:06
  • 1
    this may put you on the right path https://stackoverflow.com/a/3309904/3225 – kenny Feb 22 '18 at 19:08
  • @kenny This definitely put me on the right path, got it working as intended! :) Basically what I did was put all of the validation stuff back in the ViewModel. I then made two additional properties `ButtonsEnabled` and bound this to all of the buttons' `IsEnabled` property. Similarly, I made `BrushColor` for the textbox. Both of these properties are modified in `ViewModel.Directory`'s setter, and then I invoke the `PropertyChanged` event handler to update the xaml. Thank you! – Chris Feb 22 '18 at 19:45
  • you're welcome and good luck! – kenny Feb 22 '18 at 19:46

0 Answers0