2015-09-04 26 views
0

該屬性在ItemsControl執行。我需要格式化或應用斜體和灰色顏色的字符串樣式。如何格式化TargetNullValue屬性?

<ItemsControl ItemsSource="{Binding Source={StaticResource SettingsViewSource}, TargetNullValue= 'No setting available'}" 
           Background="Transparent" 
           HorizontalAlignment="Stretch" 
           Focusable="False"> 
+0

添加一個對空樣式有反應的轉換器嗎? – Icepickle

+0

@Ippickle我認爲'TargetNullValue'不是'依賴屬性',因此我們不能爲它應用轉換器。 –

回答

1

定義和EmptyDataTemplate如果你想有更多的像造型/格式控制和交換機上進行基於該DataTemplate中在數據觸發器上。

對於實例

<ItemsControl ItemsSource="{Binding Source={StaticResource SettingsViewSource}}" 
            Background="Transparent" 
            HorizontalAlignment="Stretch" 
            Focusable="False"> 
    <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        //Define your data template here. 
       </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    <ItemsControl.Style> 
     <Style TargetType="ItemsControl"> 
      <Style.Triggers> 
       <Trigger Property="HasItems" Value="false" > 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate> 
           <TextBlock Text="This Control is empty"/> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ItemsControl.Style> 
<ItemsControl> 

注:使用HasItems屬性來確定是否ItemsControl的包含的項目。

1

而不是使用TargetNullValue的,只是使用的風格和對於空一個DataTrigger測試:

<Style.Triggers> 
     <DataTrigger Binding="{Binding Source={StaticResource SettingsViewSource}}" Value="{x:Null}"> 
      <Setter Property="FontStyle" Value="Italic" /> 
      <Setter Property="Background" Value="Gray" /> 
     </DataTrigger> 
    </Style.Triggers>