wpf - 按钮控件模板和 DataTriggers

标签 wpf binding controltemplate datatrigger

在我正在处理的应用程序中,我有几个要监视的项目。每个项目都有多种状态(开、关、更改)。 状态保存在模型中,窗口的 DataContext 设置为它。 每个按钮 DataContext 都设置为 SortableBindingList 中的单个项目。 在 XAML 中,我为按钮和 DataTrigerrs 创建了控制模板。 但是,有时项目仅显示第一个按钮,有时仅显示前三个按钮(取决于我在 DataTrigger 中放入的内容)。 当我检查 Snoop 时,我发现所有三个按钮都设置了正确的 DataContext,并且没有绑定(bind)错误。 我希望每个按钮都会根据“serverstate”属性的值显示其内容,但是这种情况不会发生。 我做错了什么?

控制模板转到Window.Resources:

<ControlTemplate  x:Key="buttonTemplate" TargetType="Button" >
            <Border BorderThickness="1"
                    BorderBrush="Gray"
                    Margin="2"
                    Width="40" Height="35" 
                    >
                <Border.Background>
                    <SolidColorBrush Color="Brown" />
                </Border.Background>
                <ContentPresenter  />
            </Border>

            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding serverstate}"  Value="0">
                    <Setter Property="Content">
                        <Setter.Value>
                            <TextBlock Name="a1"  Text="OFF" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" />
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding serverstate}" Value="1">
                    <Setter Property="Content">
                        <Setter.Value>
                            <TextBlock Name="a2" Text="CHNG" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" />
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding serverstate}" Value="2">
                    <Setter Property="Content">
                        <Setter.Value>
                            <TextBlock Name="a3" Text="ON" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" />
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

按钮:

<Button Name="state1" 
        DataContext="{Binding ActiveServers[0]}"
        Template="{StaticResource buttonTemplate}"
        />

<Button Name="state2" 
        DataContext="{Binding ActiveServers[1]}"
        Template="{StaticResource buttonTemplate}"
        />

<Button Name="state3" 
        DataContext="{Binding ActiveServers[2]}"
        Template="{StaticResource buttonTemplate}"
        />

<Button Name="state4" 
        DataContext="{Binding ActiveServers[3]}"
       Template="{StaticResource buttonTemplate}"
        />

<Button Name="state5" 
        DataContext="{Binding ActiveServers[4]}"
        Template="{StaticResource buttonTemplate}"
        />

这是按钮在窗口中显示的方式:

How buttons in window show up

请注意按钮 4 和 5 如何完全忽略数据上下文中的属性值。

型号:

public class Model : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Model()
    {

    }

    private SortableBindingList<Server> _activeServers;
    public SortableBindingList<Server> ActiveServers
    {
        get {
            if (_activeServers == null)
            {
                _activeServers = new SortableBindingList<Server>();
            }

            return _activeServers; 
        }
        set
        {
            _activeServers = value;
            OnPropertyChanged("serverList");
        }
    }

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

最佳答案

我认为问题是,两个按钮不能使用相同的Content。因此,每种类型的 Content 对于每个触发值都会出现一次。您可以使用 ContentTemplate 来避免这种情况。 所以代码应该如下所示(对于其他 DataTriggers 也是如此)。它允许您稍后将 DataTemplates 更改为更复杂的。

<DataTrigger Binding="{Binding serverstate}" Value="2">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="ON" VerticalAlignment="Center" 
                           HorizontalAlignment="Center" Foreground="White" />
            </DataTemplate>
        </Setter.Value>
    </Setter>   
</DataTrigger>

关于wpf - 按钮控件模板和 DataTriggers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36350603/

相关文章:

wpf - 使用 protobuf-net RuntimeTypeModel 和使用 WPF 客户端预编译时出现的问题

c# - 如何将 ComboBox 绑定(bind)到具有深层 DisplayMember 和 ValueMember 属性的通用列表?

Java的final方法使用静态绑定(bind),但JVM在编译时使用 "invokevirtual"指令

wpf - 相对源与元素名称

c# - 如何在控件模板中获取目标的属性值

WPF从后面的代码中设置命名样式元素?

c# - 在 WPF 代码中绑定(bind)路径和源

带有溢出图标的 WPF TabControl

WPF - 使用用户控件并设置设计时高度和宽度

wpf - 如何更改 MahApps.Metro 对话框样式?