2012-05-14 45 views
1

我有一個ViewModel,它包含一個布爾值的ObservableCollection。我有一個控制。 想要將我的布爾值列表可視化爲具有兩種不同顏色的矩形列表。 我無法聲明這種綁定。這裏是我的代碼:綁定:使用DataTemplate和綁定可視化未命名的布爾值列表

<UserControl.Resources> 
    <DataTemplate x:Key="DataTemplateName"> 
     <Grid Margin="12,0,0,0"> 
      <Rectangle Fill="{Binding ***PROBLEM*** 
             , Converter={StaticResource BoolToSelectionBrushConverter}"} 
             HorizontalAlignment="Right" 
             Margin="0" Width="25" 
             Height="25" VerticalAlignment="Top" 
             StrokeThickness="0"/> 
     </Grid> 
    </DataTemplate> 
</UserControl.Resources> 

<Grid x:Name="LayoutRoot"> 
<ItemsControl 
     ItemsSource="{Binding Statuses}" 
        ItemTemplate="{StaticResource DataTemplateName}" 
        Margin="0,0,8,0"> 
    <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
        <VirtualizingStackPanel Orientation="Horizontal" /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
</ItemsControl> 

我能實現它,而無需創建一個包裝?

public class Value 
{ 
    public bool IsOk{get;set;} 
} 

,並結合命名屬性:

 <Grid Margin="12,0,0,0"> 
    <Rectangle Fill="{Binding IsOk 
         , Converter={StaticResource BoolToSelectionBrushConverter}}" 
         HorizontalAlignment="Right" 
         Margin="0" Width="25" 
         Height="25" VerticalAlignment="Top" 
         StrokeThickness="0"/> 
    </Grid> 

回答

3

我沒有我的WP7開發環境得心應手目前,所以這是未經測試,但我認爲下面應該工作:

<Rectangle Fill="{Binding BindsDirectlyToSource=True, 
           Converter=BoolToSelectionBrushConverter}" 
           HorizontalAlignment="Right" 
           Margin="0" Width="25" 
           Height="25" VerticalAlignment="Top" 
           StrokeThickness="0"/> 
+0

它的工作原理,臨屋區NKS。 –

2

您可以使用這個:

<Rectangle Fill="{Binding .,Converter=BoolToSelectionBrushConverter}"... /> 
+0

它也可以,謝謝。 –