DataTrigger和Storyboard

时间:2017-12-18 14:45:32

标签: c# wpf storyboard datatrigger

我一直试着让它工作4天,请帮助我。

我有一个故事板,一切都很好,直到我需要将触发操作从按钮点击更改为Binding on boolean变量。 它全部写在模板中,我想这会使任务变得更复杂。 代码是(它仍然有效):

    <Border x:Name="panelControl" Grid.RowSpan="4" Margin="0,0,-450,0" VerticalAlignment="Stretch" HorizontalAlignment="Right" RenderTransformOrigin="0.5,0.5" Width="450" Height="275" DataContext="{Binding CurrentPanelViewModel}" >
            <Border.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Border.RenderTransform>

                <Canvas Background="#FFC6E8E6">
                    ...
                </Canvas>
            </Grid>
        </Border>

    <Grid.Triggers>
            <EventTrigger RoutedEvent="Button.Click" SourceName="AddButton2">
                <BeginStoryboard Name="StartStoryboard">
                    <Storyboard SpeedRatio="12" TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                        <DoubleAnimation 
                            Storyboard.TargetName="panelControl" 
                            Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"
                            Duration="0:0:005" From="0" To="-700" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>

现在我写了。

<Border x:Name="panelControl" Grid.RowSpan="4" Margin="0,0,-450,0" VerticalAlignment="Stretch" HorizontalAlignment="Right" RenderTransformOrigin="0.5,0.5" Width="450" Height="275" DataContext="{Binding Path=Controller.LegViewModel.CurrentPanelViewModel}" >
            <Border.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Border.RenderTransform>

            <Grid.Style>
                    <Style>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path = PanelOpened}">
                                <DataTrigger.EnterActions>
                                    <BeginStoryboard >
                                        <Storyboard SpeedRatio="12" TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                                            <DoubleAnimation
                                                Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"
                                                Duration="0:0:005" From="0" To="-700" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </DataTrigger.EnterActions>

                                <!--<DataTrigger.ExitActions>
                                     you could animate close too if you wanted 
                                </DataTrigger.ExitActions>-->
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Style>

                <Canvas Background="#FFC6E8E6">
                    ....
                </Canvas>
            </Grid>
        </Border>

没有任何反应。尽管事实上我检查了PanelOpened通知。

---------- ----------- UPDATE

我在DataTrigger中添加了Value =“True”,现在我的OnPropertyChanged出错了(当propertyName = PanelOpened时):

public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        //если PropertyChanged не нулевое - оно будет разбужено
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public bool PanelOpened
    {
        get { return _panelOpened; }
        set
        {
            _panelOpened = value;
            OnPropertyChanged();
        }
    }

错误: System.InvalidOperationException:''[Unknown]'属性不指向路径'(0)中的DependencyObject。(1)[3]。(2)'。'

2 个答案:

答案 0 :(得分:0)

当绑定的值等于您指定的某个值时,将触发数据触发器。但是你没有指定价值。如果您希望触发器在PanelOpened的值变为true时“触发”,请说明:

<DataTrigger Binding="{Binding PanelOpened}" Value="True">

答案 1 :(得分:0)

事情是我尝试将DataTrigger添加到网格样式而不是边框​​。工作版:

.tg {
  border-collapse: collapse;
  border-spacing: 0;
}

.tg td {
  font-family: Arial, sans-serif;
  font-size: 14px;
  padding: 10px 5px;
  border-style: solid;
  border-width: 1px;
  overflow: hidden;
  word-break: normal;
}

.tg th {
  font-family: Arial, sans-serif;
  font-size: 14px;
  font-weight: normal;
  padding: 10px 5px;
  border-style: solid;
  border-width: 1px;
  overflow: hidden;
  word-break: normal;
}

.tg .tg-baqh {
  text-align: center;
  vertical-align: top
}

.tg .tg-edkd {
  color: #9a0000;
  text-align: center;
  vertical-align: top
}

.tg .tg-kxdw {
  font-weight: bold;
  background-color: #036400;
  color: #ecf4ff;
  text-align: center;
  vertical-align: top
}

.tg .tg-24w0 {
  font-weight: bold;
  font-size: 22px;
  background-color: #ffffff;
  text-align: center;
  vertical-align: top
}

.tg .tg-9ewa {
  color: #fe0000;
  text-align: center;
  vertical-align: top
}

.tg .tg-yw4l {
  vertical-align: top
}

.tg .tg-cd3k {
  font-weight: bold;
  background-color: #036400;
  color: #ffffff;
  text-align: center;
  vertical-align: top
}

.tg .tg-zlvv {
  color: #3531ff;
  text-align: center;
  vertical-align: top
}