robertk
robertk

Reputation: 2461

Bind to single object in viewmodel

I got this property in my viewmodel:

        private PushPinModel selectedPushPinModel;
    public PushPinModel SelectedPushPinModel
    {
        get { return selectedPushPinModel; }
        set
        {
            selectedPushPinModel = value;
            RaisePropertyChanged(() => SelectedPushPinModel);
        }
    }

And I want to bind the view to show which one is selected:

        <ContentControl DataContext="{Binding SelectedPushPinModel}" VerticalAlignment="Top">
        <ContentControl.ContentTemplate>
            <DataTemplate>
                <Grid Height="100" VerticalAlignment="Top">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="20*"/>
                        <RowDefinition Height="38*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="91*"/>
                        <ColumnDefinition Width="389*"/>
                    </Grid.ColumnDefinitions>
                    <Border Opacity="0.95" Width="480" Padding="0,0,0,0" BorderThickness="0" HorizontalAlignment="Left" BorderBrush="Transparent" Background="White" Grid.ColumnSpan="2" Grid.RowSpan="2"/>
                    <Image Width="70" Height="70" HorizontalAlignment="Center" VerticalAlignment="Center" Source="{Binding Icon}" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" />
                    <TextBlock Text="{Binding Header}" Grid.Column="1" Grid.Row="0" Style="{StaticResource TextboxLabelStyle}"/>
                    <TextBlock Text="{Binding Body}" Grid.Column="1" Grid.Row="1" Style="{StaticResource DefaultTextBlockStyle}"/>
                </Grid>
            </DataTemplate>
        </ContentControl.ContentTemplate>
    </ContentControl >

However I can't get it to work. The bindings is not showing in the view and I'm not getting any binding errors. Is it the correct way to bind to a single object? I much rather having it like this instead of binding directly with {Binding SelectedPushPinModel.Body} which more dirty.

Any suggestions how to accomplish this? Thanks

Upvotes: 0

Views: 122

Answers (2)

robertk
robertk

Reputation: 2461

Solved it, replaced DataContext on the ContentControl with Content

<ContentControl Content="{Binding SelectedPushPinModel}" VerticalAlignment="Top">

Thanks to sircodesalot for pointing me to the right direction!

Upvotes: 1

sircodesalot
sircodesalot

Reputation: 11439

Try this:

<Label Content="{Binding SelectedPushPinModel}" />

See what that gives you (if anything). Also, check the 'output' window for errors.

This line:

<ContentControl DataContext="{Binding SelectedPushPinModel}">

will only work if the DataContext for the ViewModel (Class) has been properly bound higher on the page. If it isn't then the Label binding provided above will show blank.

Upvotes: 1

Related Questions