我有一個ListBox
這樣的:如果綁定值爲空,則隱藏項目?
<ListBox VerticalAlignment="Stretch"
ItemsSource="{Binding EventInfo}">
裏面我有一個DataTemplate
與TextBlock的列表:
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="20"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="Event:" FontWeight="Bold" Grid.Column="0" Grid.Row="0"/>
<TextBlock Text="{Binding Name}" FontWeight="Bold" Grid.Column="1" Grid.Row="0"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
什麼,我需要做的,例如當隱藏的文本塊,綁定的值是null
。我有聯想這個東西:
Event: foo
但在某些情況下,Name
是空的,我需要隱藏兩TextBlock
Event
和綁定的,是可能做到這一點?
UPDATE:
我想這樣做純粹在XAML爲:
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
的問題就這麼在中斷模式和XAML錯誤的應用程序秋天,我得到:
System.Windows.Data Error: 40 : BindingExpression path error: 'Name' property not found on 'object' ''Style' (HashCode=7)'.
這個錯誤是爲每個文本塊重複我已經做錯了什麼?
是不可能做到這一點只在純XAML? – AgainMe
是的,你可以在純xaml中定義數據觸發器,但我個人發現setter方法更清晰和更強大......我將編輯我的答案以插入xaml觸發器的示例以及 –
你能否看到我的更新?我試圖在xaml中完成這項工作,但我想我的代碼中有些問題。 – AgainMe