我有一個Textblock
,當鼠標懸停在上面時,我想打開一個Popup
。我已使用MultiBinding
至Popup
的IsMouseOver
和TextBlock
的IsMouseOver
將IsOpen
屬性綁定,並且它工作正常,除非將鼠標從文本移至彈出窗口時,彈出式窗口會閃爍。WPF .NET Popup - 懸停時打開,如果鼠標懸停,請保持打開
閃爍的原因是事件的執行的發動機罩下的順序:
鼠標移動從textblock
到popup
- >IsMouseOver
的textblock
設置爲false
- >轉換器被稱爲與兩個參數是假的 - >才把popup
IsMouseOver
設置爲true
- >轉換器執行有兩個參數是假的,彈出窗口消失了 - >轉換稱爲和執行因爲另一個事件是因爲彈出的IsMouseOver
提前彈出,此時IsMouseOver的Popup
True
- >彈出窗口再次出現。我曾嘗試添加StaysOpen=False
,但它從未關閉/行爲與預期不同。
問題:我該如何避免閃爍?
代碼:
<Grid>
<ListBox ItemsSource="{Binding RandomNames}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="Name: "
Grid.Column="0"/>
<TextBlock Grid.Column="1"
x:Name="NameBlock"
Text="{Binding}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<Popup x:Name="PopupX"
Grid.Column="1"
PlacementTarget="{Binding ElementName=NameBlock}"
Placement="Bottom">
<!--<Popup.IsOpen>
<MultiBinding Converter="{StaticResource PopupIsOpenConverter}">
<Binding ElementName="PopupX" Path="IsMouseOver" Mode="OneWay" />
<Binding ElementName="NameBlock" Path="IsMouseOver" Mode="OneWay" />
</MultiBinding>
</Popup.IsOpen>-->
<Popup.Style>
<Style TargetType="Popup">
<Setter Property="IsOpen" Value="True" />
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsMouseOver, ElementName=NameBlock}" Value="False" />
<Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="False" />
</MultiDataTrigger.Conditions>
<Setter Property="IsOpen" Value="False" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Popup.Style>
<TextBlock Text="{Binding}"
Foreground="Coral" />
</Popup>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
轉換代碼
[ValueConversion(typeof(bool), typeof(bool))]
public class PopupIsOpenConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.Any(value => value is bool && (bool) value);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new ActionNotSupportedException();
}
}