2013-08-06 32 views
0

我在視圖模型得到這個屬性:綁定到單個對象在視圖模型

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

我想視圖結合,以顯示選擇其中之一:

 <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 > 

但是我做不到讓它工作。綁定沒有顯示在視圖中,我沒有收到任何綁定錯誤。它是綁定到單個對象的正確方法嗎? 我更喜歡這樣做,而不是直接綁定{Binding SelectedPushPinModel.Body}哪個更髒。

任何建議如何做到這一點? 感謝

回答

1

解決了這個問題,更換的DataContext在ContentControl中與內容

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

感謝sircodesalot指着我正確的方向!

1

試試這個:

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

看看,給你(如果有的話)。另外,檢查'輸出'窗口是否有錯誤。

這條線:

<ContentControl DataContext="{Binding SelectedPushPinModel}"> 

如果視圖模型(類)的DataContext已經在頁面上被正確綁定更高才有效。如果不是那麼上面提供的Label綁定將顯示爲空白。

+0

是的標籤綁定工作正常。我如何使它與contentcontrol一起工作? – robertk