我在同一個視窗上有兩個 ListView 控制元件。我正在嘗試對其進行樣式設定,以便一次只有一個 ListViewItem 具有焦點。我做了一個名為 FocusBehavior 的附加行為。
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace FocusTest
{
public class FocusBehavior
{
#region IsFocused Property
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached("IsFocused",
typeof(bool),
typeof(FocusBehavior));
public static bool GetIsFocused(DependencyObject d)
=> (bool)d.GetValue(IsFocusedProperty);
public static void SetIsFocused(DependencyObject d, bool value)
=> d.SetValue(IsFocusedProperty, value);
#endregion
#region SetFocus Property
public static readonly DependencyProperty SetFocusProperty =
DependencyProperty.RegisterAttached("SetFocus",
typeof(bool),
typeof(FocusBehavior),
new PropertyMetadata(HandleSetFocusedPropertyChanged));
private static void HandleSetFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var shouldSetFocus = (bool)e.NewValue;
if (shouldSetFocus == false)
{
return;
}
if (d is ListViewItem item)
{
var listView = FindAncestor<ListView>(item);
listView.LostKeyboardFocus = (sender, e) =>
{
SetIsFocused(d, false);
};
item.GotFocus = (sender, e) =>
{
SetIsFocused(d, true);
Keyboard.Focus(item);
e.Handled = true;
};
item.LostFocus = (sender, e) =>
{
SetIsFocused(d, false);
};
}
}
public static bool GetSetFocus(DependencyObject d)
=> (bool)d.GetValue(SetFocusProperty);
public static void SetSetFocus(DependencyObject d, bool value)
=> d.SetValue(SetFocusProperty, value);
#endregion
private static T FindAncestor<T>(DependencyObject dependencyObject) where T : DependencyObject
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindAncestor<T>(parent);
}
}
}
如果 ListViewItem 具有焦點并且其父級也是當前聚焦的 ListView,這允許我將文本設定為粗體。
<Window.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Focusable" Value="True"/>
<Setter Property="local:FocusBehavior.SetFocus" Value="True"/>
<Style.Triggers>
<Trigger Property="local:FocusBehavior.IsFocused" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" ItemsSource="{Binding SourceItems}"/>
<ListView Grid.Column="1" ItemsSource="{Binding TargetItems}"/>
</Grid>
這幾乎可以完美運行,但有時單擊某個專案無法使其成為焦點。還有其他方法或我缺少的東西嗎?請記住,這是 POC 代碼。
uj5u.com熱心網友回復:
如果您只想更改 ListViewItem 的外觀,系結到 ListViewItem 的屬性和父 ListView 的屬性的 MultiDataTriggerIsFocused將IsKeyboardFocusWithin是一個簡單的解決方案。
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="FontWeight" Value="Normal"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsFocused}" Value="True"/>
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListView}}, Path=IsKeyboardFocusWithin}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="FontWeight" Value="Bold"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/438277.html
