2015-04-07 86 views
1

我在ItemsControl中有一個Border,它需要從ItemsSource(Observable Collection)中名爲BorderColor的字段中獲取它的BorderBrush。在itemscontrol中的wpf borderbrush綁定

<ItemsControl ItemsSource="{Binding ObsColOfThings}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemContainerStyle> 
     <Style TargetType="ContentPresenter"> 
      <Setter Property="ContentTemplate"> 
       <Setter.Value> 
        <DataTemplate> 
         <Border BorderThickness="3" BorderBrush="{Binding BorderColor}"> 
          <ContentPresenter Content="{Binding}" /> 
         </Border> 
        </DataTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Border > 
       <TextBlock Text="{Binding Text}" /> 
      </Border> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

綁定正在工作,但邊界刷沒有。

我也嘗試這樣做,它不會盡管做工精細

<ItemsControl ItemsSource="{Binding Appointments}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Border BorderBrush="{Binding BorderColor" BorderThickness="3"> 
       <TextBlock Text="{Binding AppointmentDescription}" /> 
      </Border> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

我要去哪裏錯了文字工作,要麼?

回答

1

因爲ItemTemplate有效地替換了ContentTemplate,所以同時指定ItemsControl的ItemTemplate和ContentPresenter的ContentTemplate沒有任何意義。

刪除整個ItemContainerStyle,寫你的ItemsControl這樣的:

<ItemsControl ItemsSource="{Binding ObsColOfThings}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Border BorderThickness="3" BorderBrush="{Binding BorderColor}"> 
       <TextBlock Text="{Binding Text}" /> 
      </Border> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

的類型BorderColor屬性應該是Brush

public class Item 
{ 
    public string Text { get; set; } 
    public Brush BorderColor { get; set; } 
} 

,然後它當然應該叫BorderBrush,不BorderColor

相關問題