标签: datatrigger

WPF - 令人困惑的 DataTrigger/DoubleAnimation 行为

我正在尝试根据我班级上布尔值的绑定来为 a的ScaleY属性设置动画。当值第一次被(应用程序首次启动时)视为 false时,当我第一次在复选框的选中事件中将其更改为 true时,动画发生,但当我在同一个复选框的未选中事件中将其设置为 false 时,不会发生动画。LayoutTransformDataTriggerViewModelDataTrigger

下面列出了我正在做的事情的简化版本。

ViewModel 类非常简单,包含一个DependencyProperty名为 Selected 的布尔值。

    public class VM : DependencyObject
{
    public bool Selected
    {
        get { return (bool)GetValue(SelectedProperty); }
        set { SetValue(SelectedProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Selected.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SelectedProperty =
        DependencyProperty.Register("Selected", typeof(bool), typeof(VM), new UIPropertyMetadata(false));
}
Run Code Online (Sandbox Code Playgroud)

Window.xaml包含一个按钮和一个复选框。当复选框被选中时,当它被取消选中时,我将 ViewModel 的“Selected”属性设置为 true 和 false。这是 xaml 和代码隐藏的代码。

<Window x:Class="DataTriggers.Window1" …
Run Code Online (Sandbox Code Playgroud)

wpf datatrigger layouttransform

1
推荐指数
1
解决办法
6445
查看次数

WPF中的用法DataTrigger

我有一个TextBox在XAML中定义的控件,我想TextBox根据其IsReadOnly或IsEnabled属性应用不同的背景颜色.我使用dataTriggers实际切换颜色,如下所示:

<Style x:Key="TextBoxStyle" TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsEnabled}" Value="True">
            <Setter Property="TextBox.Background" Value="Yellow"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding IsReadOnly}" Value="True">
            <Setter Property="TextBox.Background" Value="Red"/>
        </DataTrigger>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsReadOnly}" Value="True"/>
                <Condition Binding="{Binding IsEnabled}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <Setter Property="Background" Value="Green"/>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

TextBox的定义如下:

  <TextBox Name="sourceTextBox"  Margin="5,3,5,3" IsReadOnly="True" Style="{StaticResource TextBoxStyle}" />
Run Code Online (Sandbox Code Playgroud)

但问题是,颜色没有正确应用.

上述方法有问题吗?

wpf xaml datatrigger multidatatrigger

1
推荐指数
1
解决办法
8060
查看次数

根据布尔值更改椭圆的颜色

<Grid Grid.Row="1" Width="500" Height="500">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Ellipse Fill="Red" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25"/>
    <Ellipse Fill="Red" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25" Grid.Row="1"/>
    <Ellipse Fill="Red" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25" Grid.Row="3"/>
    <Ellipse Fill="Red" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25" Grid.Column="4"/>
    <Ellipse Fill="Red" HorizontalAlignment="Center" Height="25" Margin="0,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="25" Grid.Column="4" Grid.Row="4"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

鉴于上述XAML,我希望当属性为true时,点为绿色.我假设我是用DataTrigger做的,但我能想到的唯一方法就是为每个椭圆重复它.这对我来说似乎是个hackish,并想知道他们是否是一个更好的解决方案.每个椭圆都基于一个属性,但同样看起来像是很多重复的代码.理想情况下,我想要的是这个视图使用布尔值来反映"站点"列表的状态,以确定它们是否可用.每个状态都是单向的,并且在视图启动时不会改变.

我很擅长WPF和XAML,想出一个优雅的解决方案.每当我尝试一些东西时我都会畏缩,因为它看起来像是一个完整的黑客.

编辑:感谢@ Alastair的回答我得到了它的工作.

c# wpf datatrigger

1
推荐指数
2
解决办法
7042
查看次数

WPF - DataTrigger动画不起作用

所以我试图在ItemsControl项目删除过程中制作一些动画,ObservableCollectio<Item> 我知道我无法在卸载事件中执行此操作,因为执行任何动画都为时已晚,所以我尝试使用DataTrigger

我的xaml文件看起来像这样:

 <DataTemplate DataType="{x:Type MyApp:Item}">
        <Border x:Name="ItemBorder">
            <Label Content="{Binding Path=Name}" />
        </Border>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=Removing}" Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Duration="0:0:1" From="1.0" To="0.0"
                                           Storyboard.TargetProperty="(Border.Opacity)" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger>
        </DataTemplate.Triggers>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

而我的Item课很简单:

public class Item : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private bool removing;
    public bool Removing {
        get
        {
            return removing;
        }
        set
        {
            removing = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Removing"));
        }
    }

    // same with `Name` …
Run Code Online (Sandbox Code Playgroud)

c# wpf animation datatrigger

1
推荐指数
1
解决办法
4453
查看次数

根据ComboBox设置工具提示

我想根据所选的ComboBox值设置文本框的工具提示

这是我的ComboBox:

<ComboBox x:Name="LandComboBox" >
    <ComboBoxItem Content="Test 1.2." IsSelected="True"/>
    <ComboBoxItem Content="Test 55" />
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

这是我的TextBox:

<TextBox x:Name="LandTextBox" >
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=LandComboBox, Path=SelectedItem}" Value="Test 1.2.">
                    <Setter Property="ToolTip" Value="Hello 1.2." />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=LandComboBox, Path=SelectedItem}" Value="Test 55">
                    <Setter Property="ToolTip" Value="Hello 55" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

但是没有出现工具提示.怎么了?

c# wpf datatrigger

1
推荐指数
1
解决办法
218
查看次数

在wpf中重用样式触发器

我对此做了一些研究,但未能找到如何重用样式触发器。

我在标签上设置了样式触发器,并使用数据触发器来设置内容。我在同一视图和不同视图中有多个标签。数据绑定具有相同的类型,只是其相同数据上下文的不同属性。

考虑使用以下 2 个不同的标签,我需要显示 2 个人(PersonA 和 PersonB)的表现。两个标签显示的值将基于所选的演奏格式。

<Label Grid.Row="5" Grid.Column="9"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
<Label.Style>
    <Style BasedOn="{StaticResource SomeGlobalStaticStyle}" TargetType="Label">
        <Style.Triggers>
            <DataTrigger Binding="{Binding PersonModel.PerformanceFormat}" Value="Fractional">
                <Setter Property="Content"  Value="{Binding DataModel.PersonA.Performance.Value.Fractional}"  />
            </DataTrigger>
            <DataTrigger Binding="{Binding PersonModel.PerformanceFormat}" Value="Decimal">
                <Setter Property="Content"  Value="{Binding DataModel.PersonA.Performance.Value.Decimal}" />                    
            </DataTrigger>
            <DataTrigger Binding="{Binding PersonModel.PerformanceFormat}" Value="US">
                <Setter Property="Content"  Value="{DataModel.PersonA.Performance.Value.US}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Label.Style>
Run Code Online (Sandbox Code Playgroud)

<Label Grid.Row="6" Grid.Column="9"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
<Label.Style>
    <Style BasedOn="{StaticResource SomeGlobalStaticStyle}" TargetType="Label">
        <Style.Triggers>
            <DataTrigger Binding="{Binding PersonModel.PerformanceFormat}" Value="Fractional">
                <Setter Property="Content"  Value="{Binding DataModel.PersonB.Performance.Value.Fractional}"  />
            </DataTrigger>
            <DataTrigger Binding="{Binding PersonModel.PerformanceFormat}" Value="Decimal">
                <Setter Property="Content"  Value="{Binding …
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml datatrigger

1
推荐指数
1
解决办法
1711
查看次数

Wpf ToggleButton 命令绑定数据触发器

我有一个切换按钮,它有两个绑定到按钮 IsChecked 属性的数据触发器。在数据触发器中,我设置按钮内容及其命令属性。我遇到的问题是,当您单击按钮时,数据触发器正在更改命令值,然后触发命令。是否可以先命令触发,然后再触发数据?这是代码。

 <ToggleButton x:Name="commandToggleButton" Grid.Row="0"  Height="30" HorizontalAlignment="Left" Margin="26,24,0,0"  VerticalAlignment="Top" Width="75">
                <ToggleButton.Style>
                    <Style TargetType="ToggleButton">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=commandToggleButton, Path=IsChecked}" Value="False">
                                <Setter Property="Content" Value="Build" />
                                <Setter Property="FontWeight" Value="Bold" />
                                <Setter Property="Command" Value="{Binding Build}" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=commandToggleButton, Path=IsChecked}" Value="True">
                                <Setter Property="Content" Value="Cancel" />
                                <Setter Property="FontWeight" Value="Bold" />
                                <Setter Property="Command" Value="{Binding CancelBuild}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ToggleButton.Style>
            </ToggleButton>
Run Code Online (Sandbox Code Playgroud)

我希望发生的是,当第一次单击按钮时将触发 Build 命令,然后第二次单击按钮时将触发 CancelBuild 命令。

data-binding wpf datatrigger

0
推荐指数
1
解决办法
7320
查看次数

WPF DataTrigger不起作用

我有一个按钮,其中enabled属性应该发生两次。在视图模型中,如果IsEnabled属性设置为false,则应禁用该按钮,这样效果很好。另一方面,当禁用UI中的“验证”按钮时,也应禁用该按钮,该按钮不起作用,这表示数据触发器不起作用。请帮忙。

<Button x:Name="BtnValidate" Content="Validate" Height="24" VerticalAlignment="Top" Grid.Column="2" Width="83" Command="{Binding ValidateCommand}" IsEnabled="{Binding IsValidateEnabled}" HorizontalAlignment="Left" Margin="8,28,0,0" />
    <Button Name="BtnReload" IsEnabled="{Binding IsEnabled}" HorizontalAlignment="Left" Width="123" Grid.Row="0" Grid.Column="5" Content="Reload" Command="{Binding DateCommand}" Margin="8,24,0,32">
                        <Button.Style>
                            <Style TargetType="Button">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=BtnValidate,Path=IsEnabled}" Value="False">
                                        <Setter Property="IsEnabled" Value="False"></Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                    </Button>
Run Code Online (Sandbox Code Playgroud)

c# wpf datatrigger

0
推荐指数
1
解决办法
2277
查看次数

根据视图模型属性绑定更改 MenuItem 标题和图标

我无法弄清楚如何做到这一点,人们会认为这很简单。

我有一个MenuItem是a的一部分ContextMenu。我binding在视图模型上有一个布尔属性。根据此属性的状态,我希望MenuItem更改 的标题文本和图标。

我当然可以IValueConverter为此使用 an ,但我确信使用 aDataTemplate和触发器有更优雅的解决方案。我只是想不出正确的标记。

我编写的代码(在下面剪下)有两个问题:一个HeaderTemplate似乎不包含图标,所以MenuItems's 的文本也包含图标(通常图标出现在左侧部分 - 参见图片并与复制和清除菜单项进行比较)。此外,单击MenuItem不会触发DataTemplate更改(注意该命令有效,viewmodel 绑定实际上会切换真/假状态)。

   <ContextMenu>
            <MenuItem Command="{Binding Source={x:Static cmd:Commands.PauseCommand}}"
                      CommandParameter="{Binding}">
                <MenuItem.HeaderTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image x:Name="img" Source="../Icons/pause.png"/>
                            <TextBlock x:Name="txt" Text="Pause"/>
                        </StackPanel>
                        <DataTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsPaused}" Value="True" >
                                <Setter Property="Image.Source" Value="../Icons/play.png" TargetName="img"/>
                                <Setter Property="Text" Value="Play" TargetName="txt"/>
                            </DataTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </MenuItem.HeaderTemplate>
            </MenuItem>
Run Code Online (Sandbox Code Playgroud)

菜单项

wpf datatrigger datatemplate menuitem

0
推荐指数
1
解决办法
1696
查看次数

如果超过某个值,如何更改文本框中的字体

如果超过5,我想将文本框中文本的颜色更改为蓝色.

最简单的方法是什么?

我以为会有一个简单的textbox.FontColor = Blue; 但是我找不到这样的东西

c# wpf xaml datatrigger

0
推荐指数
1
解决办法
82
查看次数