繁体   English   中英

WPF:在后面的代码中设置触发器绑定

[英]WPF: set trigger binding in code behind

我使用带有图标的矩形来显示计算状态。 我定义了一个带有数据触发器和绑定“ IsCompleted”的样式来处理计算状态(完成/失败=未完成)。 我现在有一个以上的计算,并希望使用相同类型的矩形(所有样式和行为都相同)进行多个计算。 我知道通常如何为一个矩形设置IsCompleted的绑定。
如何在特定矩形后面的代码中设置触发器的绑定(例如,calculation1.iconStyle.IsCompleted = true)? 这是否可能,或者我必须为每次计算定义一种样式(这会带来很多额外的代码)?

xaml:

<VisualBrush x:Key="NotCompleted" Stretch="Fill" Visual="{StaticResource appbar_error}" />
<VisualBrush x:Key="Completed" Stretch="Fill" Visual="{StaticResource appbar_check}" />

<Style x:Key="iconStyle" TargetType="{x:Type Rectangle}">
        <Setter Property="Width" Value="20"/>
        <Setter Property="Fill" Value="Red"/>
        <Setter Property="Height" Value="20"/>
        <Setter Property="Margin" Value="0,0,5,0"/>
        <Setter Property="OpacityMask" Value="{StaticResource NotCompleted}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsCompleted}" Value="True">
                <Setter Property="Fill" Value="Green"/>
                <Setter Property="OpacityMask" Value ="{StaticResource Completed}"/>                    
            </DataTrigger>
        </Style.Triggers>
</Style>

呼叫:

<Rectangle Name="computation1"  Style="{StaticResource iconStyle}"  Grid.Column="0" VerticalAlignment="Top" />

后面的代码:

public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    private bool _isCompleted;
    public bool IsCompleted
    {
        get { return _isCompleted; }
        set { _isCompleted= value; NotifyPropertyChanged("IsCompleted"); }
    }

private bool comp1(){
  ....
  IsCompleted = !IsCompleted;
}

编辑:

示例输出:

groupbox1:
| -------------------------
| 计算1:(TextBlock)
| 已完成或未完成的图标(矩形)
| 均值:(TextBlock)
| 其他一些东西:
| -------------------------
groupbox2:
| -------------------------
| 计算2:(TextBlock)
| 已完成或未完成的图标(矩形)
| 图:(图片)
| 其他一些东西:
| -------------------------

如果必须可视化多个计算的状态,则应该有一个ItemsControls,它绑定到计算数据项的集合。

<ItemsControl ItemsSource="{Binding Computations}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Rectangle Style="{StaticResource iconStyle}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

计算项目类可能如下所示:

public class Computation : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private bool isCompleted;
    public bool IsCompleted
    {
        get { return isCompleted; }
        set
        {
            isCompleted = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("IsCompleted"));
            }
        }
    }
}

并可以像这样在您的MainWindow类中使用:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Computations = new ObservableCollection<Computation>();
        Computations.Add(new Computation());
        Computations.Add(new Computation());
        Computations.Add(new Computation());

        DataContext = this;

        Computations[1].IsCompleted = true;
    }

    public ObservableCollection<Computation> Computations { get; set; }
}

您可以在MSDN上的“ 数据绑定概述”和“ 数据模板概述”一文中开始阅读所有有关这方面的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM