如何刷新wpf数据触发器?

发布于 2024-12-02 17:32:44 字数 1872 浏览 0 评论 0原文

我有带有故事板的网格,如下所示。

<Grid x:Name="grd_Order"   Grid.Column="2" Height="16" Margin="0,-2,0,0" Visibility="Collapsed" HorizontalAlignment="Center" VerticalAlignment="Center">
  <Grid.Resources>
    <Storyboard x:Key="stry_OrderMsgShowHide"  RepeatBehavior="3x">
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Visibility)" >
       <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
       <DiscreteObjectKeyFrame KeyTime="0:0:2" Value="{x:Static Visibility.Collapsed}"/>
     </ObjectAnimationUsingKeyFrames>
   </Storyboard>
  </Grid.Resources>
<Grid.Style>
 <Style >
   <Style.Triggers>
     <DataTrigger Value="True" Binding="{Binding Path=BlinkOrderAlert,Mode=TwoWay}">
       <DataTrigger.EnterActions>
        <BeginStoryboard x:Name="stry_BlinkOrdAlert" Storyboard="{StaticResource stry_OrderMsgShowHide}"/>
       </DataTrigger.EnterActions>
     </DataTrigger>
  </Style.Triggers>
</Style>

在我的 ViewModel.cs 中,

        private bool blinkOrderAlert;
        public bool BlinkOrderAlert
        {
            get
            {
                return blinkOrderAlert;
            }
            set
            {
                if (blinkOrderAlert == value)
                    return;
                this.blinkOrderAlert = value;
                this.RaisePropertyChanged(this, new PropertyChangedEventArgs("BlinkOrderAlert"));
            }
        }

        public void BlinkOrdAlert()
        {
                this.BlinkOrderAlert=false;
                this.BlinkOrderAlert = true;

        }
public ViewModel()
{
  this.BlinkOrderAlert=true;
}

它仅在初始化构造函数时第一次起作用。每当我调用 BlinkOrdAlert 函数时,它就不再工作了。如何修改数据触发器以在每次调用该函数时运行情节提要?谢谢。

i have the grid with storyboard as below.

<Grid x:Name="grd_Order"   Grid.Column="2" Height="16" Margin="0,-2,0,0" Visibility="Collapsed" HorizontalAlignment="Center" VerticalAlignment="Center">
  <Grid.Resources>
    <Storyboard x:Key="stry_OrderMsgShowHide"  RepeatBehavior="3x">
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Visibility)" >
       <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
       <DiscreteObjectKeyFrame KeyTime="0:0:2" Value="{x:Static Visibility.Collapsed}"/>
     </ObjectAnimationUsingKeyFrames>
   </Storyboard>
  </Grid.Resources>
<Grid.Style>
 <Style >
   <Style.Triggers>
     <DataTrigger Value="True" Binding="{Binding Path=BlinkOrderAlert,Mode=TwoWay}">
       <DataTrigger.EnterActions>
        <BeginStoryboard x:Name="stry_BlinkOrdAlert" Storyboard="{StaticResource stry_OrderMsgShowHide}"/>
       </DataTrigger.EnterActions>
     </DataTrigger>
  </Style.Triggers>
</Style>

and in my ViewModel.cs,

        private bool blinkOrderAlert;
        public bool BlinkOrderAlert
        {
            get
            {
                return blinkOrderAlert;
            }
            set
            {
                if (blinkOrderAlert == value)
                    return;
                this.blinkOrderAlert = value;
                this.RaisePropertyChanged(this, new PropertyChangedEventArgs("BlinkOrderAlert"));
            }
        }

        public void BlinkOrdAlert()
        {
                this.BlinkOrderAlert=false;
                this.BlinkOrderAlert = true;

        }
public ViewModel()
{
  this.BlinkOrderAlert=true;
}

and it only works first time when constructor is initialized. Whenever i call the BlinkOrdAlert function, it's not working anymore. How can i modify the datatrigger to run storyboard everytime i call the function? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈情不如逗狗 2024-12-09 17:32:44

考虑将以下数据触发器添加到您的样式中。
当 BlinkOrderAlert 值设置为 false 时,以下数据触发器将删除故事板;当该值设置为 true 时,它​​将添加故事板。

希望这会有所帮助。

<DataTrigger Value="False" Binding="{Binding Path=BlinkOrderAlert,Mode=TwoWay}">
                    <DataTrigger.EnterActions>
                        <RemoveStoryboard BeginStoryboardName="stry_BlinkOrdAlert"></RemoveStoryboard>
                    </DataTrigger.EnterActions>                    
                </DataTrigger>

Consider adding the following data trigger to you style.
The Following data trigger will remove the story board when the BlinkOrderAlert value set to false and when the value was true it will add story board.

hope this will help.

<DataTrigger Value="False" Binding="{Binding Path=BlinkOrderAlert,Mode=TwoWay}">
                    <DataTrigger.EnterActions>
                        <RemoveStoryboard BeginStoryboardName="stry_BlinkOrdAlert"></RemoveStoryboard>
                    </DataTrigger.EnterActions>                    
                </DataTrigger>
傲鸠 2024-12-09 17:32:44

您根本不应该真正使用 DataTrigger 来实现此目的,您尝试使用像事件这样的属性,这相当麻烦。不幸的是,本机触发器不是最佳的,因此您不能使用 EventTrigger,因为它仅支持 RoatedEvents

但是您也许可以使用 Blend 交互性中的 EventTrigger 来使用 ViewModel 事件 (Blend 3 SDK)相反,所以这可能值得一试。

You should not really use a DataTrigger for this at all, you try use a property like an event which is quite a hack. Unfortunately the native triggers are, let's say not optimal, so you cannot use an EventTrigger as it only supports RoutedEvents.

But you might be able to use ViewModel-events using the EventTrigger from Blend's Interactivity (Blend 3 SDK) instead, so that might be worth a try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文