SORU
3 HAZİRAN 2009, ÇARŞAMBA


Bir EventTrigger özellik ayarı

Bir EventTrigger bir özellik ayarlamak mümkün olmak istiyorum, bu bir takım sorunlar var.

1) EventTriggers sadece Eylemler desteği, benim özelliklerini ayarlamak için bir film şeridi kullanmak zorundayım.

2) bir film şeridi kullanırım Sonra, iki seçeneğiniz var:

  • Animasyon durdu değeri animasyon başlamadan önce geri döner Sonra . dur:
  • HoldEnd: ne kod, ne de kullanıcı etkileşimi animasyon tutan bir özelliği değiştirmek için Bu özelliği kilitlenir.

Aşağıda örnek istiyorum set İsChecked özelliği False olduğunda buton tıkladım ve istediğim kullanıcı yapabilmek için değişiklik İsChecked ve/veya olmak istiyorum mümkün değiştirmek için özellik kodu.

Örnek:

<EventTrigger
    SourceName="myButton"
    RoutedEvent="Button.Click">
    <EventTrigger.Actions>
        <BeginStoryboard>
            <Storyboard>
                <BooleanAnimationUsingKeyFrames
                    Storyboard.TargetName="myCheckBox"
                    Storyboard.TargetProperty="IsChecked"
                    FillBehavior="Stop">
                    <DiscreteBooleanKeyFrame
                        KeyTime="00:00:00"
                        Value="False" />
                </BooleanAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger.Actions>
</EventTrigger>

"Film şeridi False değerini ayarlamak için tamamlandıktan sonra. olay Tamamlanmış kullanabilirim bunun farkındayım Ancak, bu durumda bu mantık, özel bir kontrol üzerinde kullanılacak gibi XAML içinde mantık içerir, ve sadece UI özgüdür.

CEVAP
7 Kasım 2013, PERŞEMBE


Sadece kendi eylem oluşturun.

namespace WpfUtil
{
    using System.Reflection;
    using System.Windows;
    using System.Windows.Interactivity;


    /// <summary>
    /// Sets the designated property to the supplied value. TargetObject
    /// optionally designates the object on which to set the property. If
    /// TargetObject is not supplied then the property is set on the object
    /// to which the trigger is attached.
    /// </summary>
    public class SetPropertyAction : TriggerAction<FrameworkElement>
    {
        // PropertyName DependencyProperty.

        /// <summary>
        /// The property to be executed in response to the trigger.
        /// </summary>
        public string PropertyName
        {
            get { return (string)GetValue(PropertyNameProperty); }
            set { SetValue(PropertyNameProperty, value); }
        }

        public static readonly DependencyProperty PropertyNameProperty
            = DependencyProperty.Register("PropertyName", typeof(string),
            typeof(SetPropertyAction));


        // PropertyValue DependencyProperty.

        /// <summary>
        /// The value to set the property to.
        /// </summary>
        public object PropertyValue
        {
            get { return GetValue(PropertyValueProperty); }
            set { SetValue(PropertyValueProperty, value); }
        }

        public static readonly DependencyProperty PropertyValueProperty
            = DependencyProperty.Register("PropertyValue", typeof(object),
            typeof(SetPropertyAction));


        // TargetObject DependencyProperty.

        /// <summary>
        /// Specifies the object upon which to set the property.
        /// </summary>
        public object TargetObject
        {
            get { return GetValue(TargetObjectProperty); }
            set { SetValue(TargetObjectProperty, value); }
        }

        public static readonly DependencyProperty TargetObjectProperty
            = DependencyProperty.Register("TargetObject", typeof(object),
            typeof(SetPropertyAction));


        // Private Implementation.

        protected override void Invoke(object parameter)
        {
            object target = TargetObject ?? AssociatedObject;
            PropertyInfo propertyInfo = target.GetType().GetProperty(
                PropertyName,
                BindingFlags.Instance|BindingFlags.Public
                |BindingFlags.NonPublic|BindingFlags.InvokeMethod);

            propertyInfo.SetValue(target, PropertyValue);
        }
    }
}

Bu durumda bir özellik benim viewmodel üzerinde DialogResult denilen bağlayıcı ediyorum.

<Grid>

    <Button>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <wpf:SetPropertyAction PropertyName="DialogResult" TargetObject="{Binding}"
                                       PropertyValue="{x:Static mvvm:DialogResult.Cancel}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        Cancel
    </Button>

</Grid>

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • MultiPlayStationMan

    MultiPlaySta

    17 Aralık 2009
  • MofoHifi Records

    MofoHifi Rec

    15 HAZİRAN 2006
  • Tome Rodrigo

    Tome Rodrigo

    9 Temmuz 2006