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

时间:2013-10-21 12:22:33

标签: c# wpf xaml binding triggers

我使用带图标的矩形来显示计算的状态。我使用datatrigger定义了一个样式,并使用绑定“IsCompleted”来处理计算的状态(completed / failed = not completed)。我现在有多个计算,并且想要使用相同类型的rectancle(都具有相同的样式和行为)进行多次计算。 我知道如何为一个矩形设置IsCompleted的绑定 如何在特定矩形的代码后面设置触发器的绑定(例如calculate1.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)
|其他一些东西:
| -------------------------
groupbox2:
| -------------------------
|计算2 :(文本块)
|已完成或未完成(矩形)的图标
|图:(图片)
|其他一些东西:
| -------------------------

1 个答案:

答案 0 :(得分:0)

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

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

Computation项类可能如下所示:

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上的Data Binding OverviewData Templating Overview文章中开始阅读所有这些内容。