0

I'm trying to create a text preview of a file path by using MultiBinding in a TextBlock but I'm having an issue. What I would like to do is to set some values in some UI elements (one combobox and one textbox) and see the preview populating at the bottom of the window.
Preview's XAML

    <Label Grid.Row="3" Grid.Column="0" Content="Preview:"/>
    <TextBlock>
        <TextBlock.Text>
            <MultiBinding StringFormat="P:\{0}\{1}\file.txt">
                <Binding Path="SelectedStudio.Code"/>
                <Binding Path="SelectedProject.ProjectCode"/>
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>

SelectedStudio and SelectedProject are properties in the ViewModel
ViewModel

    public ObservableCollection<WbStudio> Studios { get; set; }

    public static WbStudio SelectedStudio { get; set; }

    public static WbProject SelectedProject { get; set; }

The objects

public class WbProject
{
    public string ProjectName { get; set; }
    public string ProjectCode { get; set; }
    public string ClientName { get; set; }
    public string Address { get; set; }
}

and

public class WbStudio
{
    public string Name { get; set; }
    public string Code { get; set; }
}

The properties of the ViewModel are bound to a different section of the UI at the top of the window
Setup XAML

    <Label Content="Project Studio:"/>
    <ComboBox ItemsSource="{Binding Studios, Mode=OneTime}" DisplayMemberPath="Code" SelectedValuePath="Code"
              SelectedItem="{Binding SelectedStudio, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

    <Label Content="Project Code:"/>
    <TextBox Text="{Binding SelectedProject.ProjectCode}"/>

Here is the issue:
The second Binding element in the Multibinding (<Binding Path="SelectedProject.ProjectCode"/>) will update and show up in the preview's TextBlock as soon as the TextBox above loses focus (perfect!), while the first Binding element (<Binding Path="SelectedStudio.Code"/>) never shows in the preview's TextBlock when I select an item from the ComboBox.

What am I missing and how do I fix it?
I thought it was a matter of implementing OnPropertyChanged(string info) for the WbStudio properties but that didn't work and WbProject doesn't have it implemented either so I thought that shouldn't be the issue. I also thought it might have to do with the properties being static, but they are both static and SelectedProject works. And you don't implement OnPropertyChanged methods the same way for static properties. So I'm suspecting it has to do with some difference between Combobox and TextBox?

Andrea Tassera
  • 359
  • 3
  • 17
  • I dont know how your collection looks like, but I think SelectedValuePath="Code" is wrong. This means the Thing you want to set to Bound object is Code property. later in the Multibinding you are expecting SelectedStudio.Code to exist. Get rid of this SelectedValuePath - may be that works. – Prateek Shrivastava Nov 26 '20 at 00:50
  • I just tried to get rid of it but still not working! The collection is simply a collection of that `WbStudio` objects (code in the post), but let me know if you need more info – Andrea Tassera Nov 26 '20 at 01:31
  • ViewModel has to implement the INotifyPropertyChanged interface and fire the PropertyChanged event for its properties, especially for SelectedStudio. As a note, setting `Mode=TwoWay` and `UpdateSourceTrigger=PropertyChanged` on the SelectedItem Binding is pointless. Both are set by default. – Clemens Nov 26 '20 at 06:37
  • To who ever closed this question, the posted topic doesn't answer my question. As I said in the original post, I tried with INotifyPropertyChanged and it didn't work. ALSO, SelectedStudio is a static property so you don't implement OnPropertyChanged in the same way. Please re-open the question! – Andrea Tassera Nov 26 '20 at 07:52
  • "*I tried with INotifyPropertyChanged and it didn't work*" is in no way a sufficient problem description. You have to implement that interface for non-static properties, and fire a StaticPropertyChanged event for static properties. If that doesn't work, show us what you did. Besides that, properties in a view model should never be static. – Clemens Nov 26 '20 at 08:30
  • Thanks for your reply @Clemens ! I didn't think of pasting the code for the OnStaticPropertyChanged as it wasn't working and I think that's not the problem, so I thought of avoiding confusion. But I took the code from the last two answers this other post https://stackoverflow.com/questions/4680244/binding-static-property-and-implementing-inotifypropertychanged. Anyway the SelectedProject property is **static** too but that works in populating the element, so that's why I was thinking it wasn't a matter of that – Andrea Tassera Nov 26 '20 at 09:00
  • `Path="SelectedStudio.Code"` is not valid for a static property. See the last linked original question and search StackOverflow. It should be something like `Path="(local:ViewModel.SelectedStudio).Code"`. However, these properties should not be static in the first place. – Clemens Nov 26 '20 at 09:14
  • @Clemens I tried that now and still not working. Again, SelectedProject is bound using `` and it works! That's why I was thinking it might be a difference between `ComboBox` and `TextBox`. Is it possible to re-open the question? Maybe someone is willing to help... – Andrea Tassera Nov 26 '20 at 22:14
  • Better write a new question with all relevant details, but not more. Show us only what is not working, but show all details of your implementation. Also give a reason why these properties are static. See also https://stackoverflow.com/help/minimal-reproducible-example – Clemens Nov 27 '20 at 08:22

0 Answers0