有沒有辦法在 DataGridRow 樣式定義中為 DataGridRow 附加 IsVisibleChanged 事件的處理程式?也就是說,有沒有辦法執行以下操作:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="IsVisibleChanged" Handler="OnIsVisibleChanged"/>
</Style>
</DataGrid.RowStyle>
以上將不起作用,因為 EventSetter 只能應用于 RoutedEvents 而不能應用于常規 CLR 事件,例如 IsVisibleChanged。
uj5u.com熱心網友回復:
我們必須創建一個附加屬性和一個事件。
using System;
using System.Windows;
namespace CommonCore.AttachedEvents
{
public static class UIElementHelper
{
public static readonly RoutedEvent IsVisibleChangedEvent = EventManager.RegisterRoutedEvent(
"IsVisibleChanged", RoutingStrategy.Bubble, typeof(IsVisibleChangedEventHandler), typeof(UIElementHelper));
public static void AddIsVisibleChangedHandler(DependencyObject dependencyObject, IsVisibleChangedEventHandler handler)
{
if (dependencyObject is not UIElement uiElement)
return;
uiElement.AddHandler(IsVisibleChangedEvent, handler);
}
private static void RaiseIsVisibleChangedEvent(object sender, DependencyPropertyChangedEventArgs e)
{
((UIElement)sender).RaiseEvent(new IsVisibleChangedEventHandlerArgs(IsVisibleChangedEvent, (bool)e.NewValue, (bool)e.OldValue));
}
public static void RemoveIsVisibleChangedHandler(DependencyObject dependencyObject, IsVisibleChangedEventHandler handler)
{
if (dependencyObject is not UIElement uiElement)
return;
uiElement.RemoveHandler(IsVisibleChangedEvent, handler);
}
public static bool GetRaiseIsVisibleChanged(UIElement uiElement)
{
return (bool)uiElement.GetValue(RaiseIsVisibleChangedProperty);
}
public static void SetRaiseIsVisibleChanged(UIElement uiElement, bool value)
{
uiElement.SetValue(RaiseIsVisibleChangedProperty, value);
}
// Using a DependencyProperty as the backing store for RaiseIsVisibleChanged. This enables animation, styling, binding, etc...
public static readonly DependencyProperty RaiseIsVisibleChangedProperty =
DependencyProperty.RegisterAttached(
"RaiseIsVisibleChanged",
typeof(bool),
typeof(UIElementHelper),
new PropertyMetadata(false, OnRaiseIsVisibleChanged));
private static void OnRaiseIsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not UIElement uiElement)
{
throw new InvalidOperationException("Implemented only for UIElement.");
}
if ((bool)e.NewValue)
{
uiElement.IsVisibleChanged = RaiseIsVisibleChangedEvent;
}
else
{
uiElement.IsVisibleChanged -= RaiseIsVisibleChangedEvent;
}
}
}
public class IsVisibleChangedEventHandlerArgs : RoutedEventArgs
{
public bool NewValue { get; }
public bool OldValue { get; }
public IsVisibleChangedEventHandlerArgs(RoutedEvent routedEvent, bool newValue, bool oldValue)
: base(routedEvent)
{
NewValue = newValue;
OldValue = oldValue;
}
}
public delegate void IsVisibleChangedEventHandler(object sender, IsVisibleChangedEventHandlerArgs args);
}
它們的用途:
<StackPanel>
<CheckBox x:Name="checkBox" Content="Видимость" IsChecked="False"/>
<Border Background="AliceBlue" Padding="10" Margin="10">
<Grid Height="20" Background="Aqua"
Visibility="{Binding IsChecked, ElementName=checkBox, Converter={commcnvs:BooleanToVisibility}}">
<FrameworkElement.Style>
<Style TargetType="Grid">
<Setter Property="aev:UIElementHelper.RaiseIsVisibleChanged" Value="True"/>
<EventSetter Event="aev:UIElementHelper.IsVisibleChanged" Handler="OnIsVisibleChanged"/>
</Style>
</FrameworkElement.Style>
</Grid>
</Border>
</StackPanel>
private void OnIsVisibleChanged(object sender, IsVisibleChangedEventHandlerArgs args)
{
MessageBox.Show($"OldValu = {args.OldValue}; NewValue = {args.NewValue};");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/527660.html
上一篇:如何延遲curl請求
