4

I am trying to bind two properties from different classes in DataTemplate.

<DataTemplate x:Key="DemoItemTemplate" x:DataType="local:DemoInfo">
   <NavigationViewItem Visibility="{Binding Visibility, Mode=TwoWay}" Content="{x:Bind Name}"/>
</DataTemplate>

DataType set as DemoInfo for this DataTemplate and Name value updated from DemoInfo.

I have tried view model as source and relative source binding. But Visibility property binding not working from ViewModel class. Any suggest how to achieve this?

Visibility="{Binding Visibility, Source={StaticResource viewModel}}"
0

3 Answers 3

0

AFAIK , you cant use multibinding in UWP , you can try to use Locator What is a ViewModelLocator and what are its pros/cons compared to DataTemplates?

Sign up to request clarification or add additional context in comments.

Comments

0

How to bind two different class properties in DataTemplate

If you bind Visibility with StaticResource, please declare ViewModel class in your page Resources like the following.

ViewModel

public class ViewModel
{
    public ViewModel()
    {
        Visibility = false;
    }
    public bool Visibility { get; set; }
}

Xaml

<Page.Resources>
    <local:ViewModel x:Key="ViewModel" />
</Page.Resources>


<DataTemplate x:DataType="local:Item">
        <TextBlock
            Width="100"
            Height="44"
            Text="{x:Bind Name}"
            Visibility="{Binding Visibility, Source={StaticResource ViewModel}}" />
    </StackPanel>
</DataTemplate>

Update

If you want Visibility value changed dynamically at run-time, you need implement INotifyPropertyChanged interface for ViewModel class.

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        Visibility = false;
    }
    private bool _visibility;
    public bool Visibility
    {
        get
        {

            return _visibility;
        }

        set
        {
            _visibility = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string PropertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
    }
}

For more detail please refer Data binding in depth official document.

3 Comments

Thanks, but it working fine when application loaded and not working when the Visibility value changed dynamically at run-time. I have set mode as TwoWay and UpdateSourceTrigger as PropertyChanged in xaml and RaisePropertyChanged() used for Visibility property in ViewModel class.
You need implement INotifyPropertyChanged interface for ViewModel class
@KanniyappanP, I have updated the case reply, please check.
0

I could be wrong, but I thought the Visibility property had a dedicated enum that contains all the possible options (Visibility Enum) for the Visibility property. So, your binding might be working just fine, but the Type of the bound property would need to be of type Visibility using System.Windows.

On a side note, I wouldn't put a visibility property in the view model anyway. I think a more standard approach would be to have a visibility DependencyProperty in the immediate code behind of the view for your binding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.