如何更新样式的内容?

时间:2013-02-06 12:11:25

标签: c# wpf xaml

我上课了:

class NoExp : Expander
{ 
    public long Id { get; set; }
    public string btnBackGround { get;set;}
    public bool btnEnabled { get; set; }
    public string btnForeground { get; set; }
    public string tbText { get; set; }
    public string tbTime { get; set; }
    public string tbNumber { get; set; }
    public string tbForeground { get; set; }
    public ServiceReference.PickerItemItem[] DGItemSource { get; set; }}

风格:

 <Style x:Key="ExpanderStyle" TargetType="{x:Type local:NoExp}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:NoExp}">
                    <Grid>
                        <Button Content="Собран"  Height="35.5" Margin="5,0,1,0" VerticalAlignment="Top"
                                Foreground="{Binding btnForeground, RelativeSource={RelativeSource TemplatedParent}}"
                                Background="{Binding btnBackGround, RelativeSource={RelativeSource TemplatedParent}}" 
                                IsEnabled="{Binding btnEnabled, RelativeSource={RelativeSource TemplatedParent}}" 
                                Tag="{Binding Id, RelativeSource={RelativeSource TemplatedParent}}"
                                Style="{DynamicResource ButtonStyle1}" Click="Button_Click_1"/>

在我试图改变的代码中:

  foreach (NoExp exp in listBox.Items)
                {
                    exp.btnForeground = "#808080";
                }

数据发生了变化,但界面没有变化。请告诉我该怎么做。

1 个答案:

答案 0 :(得分:0)

您是否考虑过使用可观察的馆藏?

您的实体需要继承INotifyPropertyChanged并在设置属性时调用NotifyPropertyChanged。假设您的实体对象包含在一个ObservableCollection中,您的控件绑定到该ObservableCollection,所有内容都将更新。

我在Silverlight中成功地使用它来对抗PivotViewer。

了解更多: Silverlight Databinding – The Observable Collection