是否可以将行为附加到Silverlight应用程序中的所有TextBox?
我需要为所有文本框添加简单的功能.(选择焦点事件上的所有文字)
void Target_GotFocus(object sender, System.Windows.RoutedEventArgs e)
{
Target.SelectAll();
}
Run Code Online (Sandbox Code Playgroud) 我一直在尝试在wpf窗口上实现一个行为,因此我在当前的解决方案中添加了对System.Winodws.Interactivity的引用,然后编写了所需的行为.但是为了应用这种行为,我必须在Windows XAML中编写类似的东西.
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:behav ="clr-namespace:WpfApplication5"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity;assembly=System.Windows.Interactivity">
<Window.Resources>
<i:Interaction.Behaviors>
<behav:DialogIconRemoveBehavior></behav:DialogIconRemoveBehavior>
</i:Interaction.Behaviors>
</Window.Resources>
<Grid>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
但这不是有效的标签,因为我可能必须添加对System.Windows.Interactivity的任何其他程序集的引用,所以,请建议我在XAML中使用标签时还需要做些什么
I might be getting the terminology wrong here, but I think I'm trying to create an attached event.
In the Surface SDK, you can do things like:
<Grid Background="{StaticResource WindowBackground}" x:Name="Foo" s:SurfaceFrameworkElement.ContactChanged="Foo_ContactChanged"/>
Run Code Online (Sandbox Code Playgroud)
I want to create a custom event for which a handler can be added in XAML in the same way, but I'm having trouble.
I can create a custom routed event, but the XAML intellisense doesn't see it and the event handler isn't added if I just type …
我在silverlight控件上使用了几个Blend行为和触发器.我想知道是否有任何机制可以自动分离或确保在不再使用控件时(即从可视树中删除)为行为或触发器调用OnDetaching().
我的问题是,由于其中一个行为,控件存在托管内存泄漏.该行为在OnAttached()重写中的某个长期对象上订阅一个事件,并且应该在OnDetaching()重写中取消订阅该事件,以便它可以成为垃圾收集的候选者.但是,当我从可视树中删除控件时,OnDetaching()似乎永远不会被调用...我能够实现它的唯一方法是在删除控件之前明确地分离有问题的行为,然后正确地进行垃圾回收.
现在我唯一的解决方案是在代码隐藏中为控件创建一个公共方法,该方法可以通过并分离任何可能导致垃圾收集问题的已知行为.在从面板中删除控件之前,要由客户端代码知道调用它.我真的不喜欢这种方法,所以我正在寻找一些自动的方法,这是我忽略或更好的建议.
public void DetachBehaviors()
{
foreach (var behavior in Interaction.GetBehaviors(this.LayoutRoot))
{
behavior.Detach();
}
//continue detaching all known problematic behaviors on the control....
}
Run Code Online (Sandbox Code Playgroud) silverlight garbage-collection memory-leaks attachedbehaviors expression-blend
我使用附加行为,允许将DoubleClick事件连接到视图模型中的命令,如下面的绑定:
<ListBox Style="{StaticResource MasterListBoxStyle}"
b:SelectionBehavior.DoubleClickCommand="{Binding EditCommand}"
>
Run Code Online (Sandbox Code Playgroud)
我需要多个列表框用于演示文稿,所有这些都需要连接到EditCommand的DoubleClick.
我可以将此行为推送到我的MasterListBoxStyle中吗?怎么样?
干杯,
Berryl
<Style x:Key="MasterListBoxStyle" TargetType="ListBox">
<Setter Property="ItemsSource" Value="{Binding MasterVm.AllDetailVms}" />
<Setter Property="ItemContainerStyle" Value="{StaticResource MasterListingRowStyle}" />
<Setter Property="IsSynchronizedWithCurrentItem" Value="True" />
<Setter Property="AlternationCount" Value="2" />
</Style>
Run Code Online (Sandbox Code Playgroud) 我正在WPF应用程序中实现附加行为.我需要传递一个类型参数的行为,这样我就可以调用一个方法void NewRow(Table<T> table)上SqliteBoundRow.如果我在XAML中实例化一个对象,我会使用类型参数传递x:TypeArguments,但是在设置附加行为时我没有看到这样做的方法,因为它使用静态属性.
附加行为的代码如下所示:
public abstract class SqliteBoundRow<T> where T : SqliteBoundRow<T>
{
public abstract void NewRow(Table<T> table);
}
public class DataGridBehavior<T> where T:SqliteBoundRow<T>
{
public static readonly DependencyProperty IsEnabledProperty;
static DataGridBehavior()
{
IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled",
typeof(bool), typeof(DataGridBehavior<T>),
new FrameworkPropertyMetadata(false, OnBehaviorEnabled));
}
public static void SetIsEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsEnabledProperty, value);
}
public static bool GetIsEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsEnabledProperty);
}
private static void OnBehaviorEnabled(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs args)
{
var dg = dependencyObject …Run Code Online (Sandbox Code Playgroud) 我正在向滑块添加附加行为,这会导致在拖动拇指并将其保持在特定区域时滚动某些内容.(不能使用简单的IsMouseOver触发器,因为Slider Thumb具有MouseCapture.)
该行为有3个属性:
#region IsScrollHoverProperty
public static readonly DependencyProperty IsScrollHoverProperty = DependencyProperty.RegisterAttached(
"IsScrollHover",
typeof(Boolean),
typeof(ScrollHoverAreaBehaviour),
new UIPropertyMetadata(false));
#endregion
#region ScrollLeftRectProperty
public static readonly DependencyProperty ScrollLeftRectProperty = DependencyProperty.RegisterAttached(
"ScrollLeftRect",
typeof(Rectangle),
typeof(ScrollHoverAreaBehaviour),
new UIPropertyMetadata(null));
#endregion
#region ScrollRightRectProperty
public static readonly DependencyProperty ScrollRightRectProperty = DependencyProperty.RegisterAttached(
"ScrollRightRect",
typeof(Rectangle),
typeof(ScrollHoverAreaBehaviour),
new UIPropertyMetadata(null));
#endregion
Run Code Online (Sandbox Code Playgroud)
当用户拖动滑块时,IsScrollHoverProperty被设置为true,这一切都在Slider的ControlTemplates.Triggers中完成,并且可以正常工作.
当它设置为true时,回调将把PreviewMouseEnterHandlers挂钩到两个Rectangle中以检测鼠标何时进入它们.
有问题的矩形也在Slider的controltemplate中定义:
<StackPanel Grid.Row="0" Grid.RowSpan="3" HorizontalAlignment="Left" Orientation="Horizontal">
<Rectangle Width="40" Fill="#AAAAAAAA" Name="ScrollLeftRect"/>
</StackPanel>
<StackPanel Grid.Row="0" Grid.RowSpan="3" HorizontalAlignment="Right" Orientation="Horizontal">
<Rectangle Width="40" Fill="#AAAAAAAA" Name="ScrollRightRect"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是将这些矩形绑定到附加的ScrollRightRect和ScrollLeftRect属性.我尝试了一些事情,并怀疑我犯了一个愚蠢的绑定错误,或者我试图做一些不允许的事情.我目前正在controltemplate.triggers中绑定它们,如下所示:
<Trigger Property="local:ScrollHoverAreaBehaviour.IsScrollHover" Value="False">
<Setter Property="local:ScrollHoverAreaBehaviour.ScrollLeftRect" Value="{Binding ElementName=ScrollLeftRect}"/>
<Setter …Run Code Online (Sandbox Code Playgroud) 我尝试VisualStateManager.GetVisualStateGroups在OnAttached自定义行为的覆盖中使用,以及在添加到AssociatedObject.Loaded该行为中的事件的事件处理程序中使用。两次我都得到一个空列表。
是否有另一种方法来获取为控件定义的视觉状态组,或者我应该附加到的另一个事件处理程序?
因为它被问到了,是的,控件有VisualStateGroups和VisualStates。
c# silverlight visualstatemanager attachedbehaviors silverlight-4.0
我试图做一个附加行为来重新排序行bby做 Drag & Drop
我发现了一些解决方案(在Stackoverflow上和通过谷歌搜索)并使用它们我试图做出行为......我从Hordcodenet网站上拿了例子(我现在没有链接)
public static class DragDropRowBehavior
{
private static DataGrid dataGrid;
private static Popup popup;
private static bool enable;
private static object draggedItem;
public static object DraggedItem
{
get { return DragDropRowBehavior.draggedItem; }
set { DragDropRowBehavior.draggedItem = value; }
}
public static Popup GetPopupControl(DependencyObject obj)
{
return (Popup)obj.GetValue(PopupControlProperty);
}
public static void SetPopupControl(DependencyObject obj, Popup value)
{
obj.SetValue(PopupControlProperty, value);
}
// Using a DependencyProperty as the backing store for PopupControl. This enables animation, styling, binding, etc... …Run Code Online (Sandbox Code Playgroud) 我有一个InvokeCommandAction与我喜欢的GotFocus事情相关的TextBox事情:
<TextBox Grid.Row="0"
Grid.Column="1"
Width="40"
HorizontalAlignment="Right">
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<i:InvokeCommandAction Command="{Binding GotFocusCommand}" CommandParameter="Enter data [message to be displayed]" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
它的工作方式很好,但是我有几十个TextBox这样的设置.而不是重复代码(正如我目前为每个代码所做的那样),我希望将该触发器附加到所有类型的控件{x:Type TextBox}.
通常,我会在该Resources部分设置属性,如下所示:
<UserControl.Resources>
<Style TargetType="TextBlock">
<Setter Property="Padding" Value="5,0,0,0" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不适用于Triggers:
附加属性"触发器"只能应用于从"DependencyObject"派生的类型.
理想情况下,我想做这样的事情:
<UserControl.Resources>
<Style TargetType="{x:Type TextBox}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="GotFocus">
<i:InvokeCommandAction Command="{Binding GotFocusCommand}" CommandParameter="{Binding Tag}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
然后我只需要设置Tag每个属性TextBox来指定要显示的消息.我是在正确的轨道上吗?我是否需要将其更改为使用ControlTemplate或类似的东西?
编辑 …
wpf ×7
c# ×5
silverlight ×4
xaml ×2
binding ×1
blend ×1
events ×1
memory-leaks ×1
mvvm ×1
styles ×1
wpfdatagrid ×1