我在軟體中使用模板自定義了listviewitem的樣式,其中根據背景關系對某項進行了隱藏,但是在界面中依然能夠選中隱藏的那項

代碼如下
<Style TargetType="ListView">
<!--重定義ListBox中項的樣式-->
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Border Width="80" Margin="3" BorderBrush="SteelBlue"
BorderThickness="1" CornerRadius="5"
Visibility="{Binding Converter={StaticResource DeviceVisibilityConverter}}"
HorizontalAlignment="Center" VerticalAlignment="Center"
Background="{Binding Path=Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}">
<TextBlock
HorizontalAlignment="Center" VerticalAlignment="Center"
Text="{Binding Converter={StaticResource DeviceIdConverter},StringFormat=Id:{0}}"/>
</Border>
</DataTemplate>
</Setter.Value>
</Setter>
<!--自定義選中項的顏色-->
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="ListViewItem.IsSelected" Value="https://bbs.csdn.net/topics/True">
<Setter Property="ListViewItem.Background" Value="https://bbs.csdn.net/topics/Green"/>
</Trigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
</Style>
uj5u.com熱心網友回復:
并且這個應該被隱藏的項還能被選中我在border中使用Visibility="{Binding Converter={StaticResource DeviceVisibilityConverter}}"進行了隱藏,但感覺好像只是將模板進行了隱藏ListviewItem沒有被隱藏
uj5u.com熱心網友回復:
轉換器代碼發一下看看 回傳屬性設定為Collapsed試試uj5u.com熱心網友回復:
回傳屬性就是Collapsed
CommunicationModel是我內部定義的一個類,我斷點試過,確實回傳了Collapsed,感覺好像就是border被隱藏啦,但是listviewItem還是有
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is CommunicationModel)
return Visibility.Visible;
else
{
return Visibility.Collapsed;
}
}
uj5u.com熱心網友回復:
斷點除錯value值 看是否是value值傳入的原因
uj5u.com熱心網友回復:
斷點除錯value值 看是否是value值傳入的原因
不是的,value值是正確的,這個按照了預期的流程,前幾個回傳的是Visible最后一個回傳了Collapsed,應該不是這個問題,因為我強制把border 得visibility設定為Collapsed 然后所有子項都是這個情況,所以我懷疑是樣式問題
<Border Width="80" Margin="3" BorderBrush="SteelBlue"
BorderThickness="1" CornerRadius="5"
Visibility="Collapsed"
HorizontalAlignment="Center" VerticalAlignment="Center"
Background="{Binding Path=Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}">
<TextBlock
HorizontalAlignment="Center" VerticalAlignment="Center"
Text="{Binding Converter={StaticResource DeviceIdConverter},StringFormat=Id:{0}}"/>
</Border>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/69558.html
標籤:C#
上一篇:單項鏈表節點的插入
下一篇:C# 串口通訊
