在XAML的codeehind中设置值
本文关键字:设置 codeehind XAML | 更新日期: 2023-09-27 18:26:59
我的xaml.cs
:中有以下变量
bool _isdragging = false;
现在我想从下面的xaml
代码中设置_isdragging
的值:
<ControlTemplate.Triggers>
<Trigger Property="IsDragging" Value="true">
<!--set _isdragging to true-->
</Trigger>
<Trigger Property="IsDragging" Value="false">
<!--set _isdragging to false-->
</Trigger>
</ControlTemplate.Triggers>
我怎样才能做到这一点?在xaml
/wpf
中如何做到这一点?我只找到了关于获取/绑定值(到控件)的文章,但没有一篇处理设置它…
感谢您的帮助!谢谢
不,您有一个字段,而不是property。你可以使用这样的设置器:
<Trigger Property="IsDragging" Value="true">
<Setter Property="IsDragging" Value="True" />
</Trigger>
但是IsDragging应该是你的控制的依赖属性
您可以在setters中使用绑定:
<Setter Property="IsDragging"
Value="{Binding AnotherProperty, RelativeSource={RelativeSource Self}}" />
更新
对于您的场景,您可以使用变通方法从另一个控件访问简单的公共字段。添加Behavior类并绑定到其值:
<Setter Property="behaviours:IsDraggingBehaviour.IsDragging" Value="True"/>
然后在你的行为课上:
public static class IsDraggingBehaviour
{
public static bool GetSelectAll(YourControl target)
{
return (bool)target.GetValue(IsDraggingAttachedProperty);
}
public static void SetSelectAll(YourControl target, bool value)
{
target.SetValue(IsDraggingAttachedProperty, value);
}
public static readonly DependencyProperty IsDraggingAttachedProperty = DependencyProperty.RegisterAttached("IsDragging", typeof(bool), typeof(YourControl), new UIPropertyMetadata(false, OnSelectIsDraggingPropertyChanged));
static void OnSelectIsDraggingPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var control = (YourControl) o;
//control.AccessYourProperty = true; change your value here
}
}
但我认为有一种更好的方法可以通过更改组合或使用WPF功能(如依赖属性和清晰绑定)来解决您的问题。你可以试着扩展你的问题。