【问题标题】:Issue binding a boolean to the IsEnabled of a button将布尔值绑定到按钮的 IsEnabled 问题
【发布时间】:2018-10-01 22:00:08
【问题描述】:

我发现了一些方向相同的问题,但可以直接找到适合我的问题的任何问题。

我有一个列表中有一个 2D 按钮数组。现在我想将“IsEnabled”状态绑定到类的属性。 “标签”已经从该类的对象中填充了正确的值,但无论我尝试什么,“IsEnabled”都不起作用。

这是我正在使用的 DataTemplate 的 XAML:

    <DataTemplate x:Key="DataTemplate_Level2">
        <Button Height="35" Width="35" Margin="-1,-1,-1,-1" IsEnabled="{Binding hit}" x:Name="fieldButton" Click="fieldClick" Tag="{Binding}" Style="{StaticResource ButtonStyle1}"/>
    </DataTemplate>

    <DataTemplate x:Key="DataTemplate_Level1">
        <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_Level2}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DataTemplate>

这是填充列表的代码:

fields=new List<List<Field>>();

for(int i = 0; i < SIZE; i++) {
    fields.Add(new List<Field>());
    for(int j = 0; j < SIZE; j++) {
        fields[i].Add(new Field(i, j));
    }
}

这是我希望“hit”属性绑定到“IsEnabled”的类: 私人int x; private int y;

    public Boolean hit;

    public int X { get => this.x; set => this.x = value; }
    public int Y { get => this.y; set => this.y = value; }

    public Field(int x,int y) {
        this.x = x;
        this.y = y;
        this.hit = false;
    }

    public override string ToString() {
        return this.x + "," + this.y;
    }

【问题讨论】:

    标签: c# wpf button binding isenabled


    【解决方案1】:

    hit 是您代码中的一个字段。您只能绑定到一个属性。

    尝试将其更改为这样的属性:

    public bool hit { get; set; }
    

    【讨论】:

    • ... 并且该类必须实现 INotifyPropertyChanged 并且属性的每次更改都必须引发 PropertyChanged 事件
    猜你喜欢
    • 1970-01-01
    • 2014-10-17
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多