关于按钮内容的If语句.WPF

本文关键字:If 语句 WPF 于按钮 按钮 | 更新日期: 2023-09-27 18:02:53

我有listview一个按钮(默认内容== "添加")和1个文本框。在列表视图中双击事件按钮。内容=="编辑"和文本框填充来自listview的信息。

这个if语句不能正常工作

            if (btnAddFloor.Content == "Add")
            {
                HProDataContext db = new HProDataContext();
                floor f = new floor { floorname = txtFloorName.Text };
                db.floors.InsertOnSubmit(f);
                db.SubmitChanges();
                RefreshListViewFloor();
            }
            else
            {
                int currentID = Convert.ToInt32(listViewFloors.SelectedValue);
                HProDataContext db = new HProDataContext();
                floor f = db.floors.Single(p => p.id == currentID);
                f.floorname = txtFloorName.Text;
                db.SubmitChanges();
                RefreshListViewFloor();
                txtFloorName.Text = null;
                RefreshListViewRoom();
            }

和btnAddFloor。Content == "Add"为绿色下划线有人能帮我吗?

XAML

    <GroupBox Header="Floors" Height="287" HorizontalAlignment="Left" Margin="12,32,0,0" Name="GroupBoxFloors" VerticalAlignment="Top" Width="342">
        <Grid>
            <Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="169,37,0,0" Name="btnAddFloor" VerticalAlignment="Top" Width="75" Click="btnAddFloor_Click" />
            <ListView Height="192" HorizontalAlignment="Left" Margin="6,66,0,0" Name="listViewFloors" VerticalAlignment="Top" Width="318" ItemsSource="{Binding FloorCollection}" SelectedValuePath="ID" MouseDoubleClick="listViewFloors_MouseDoubleClick">
                <ListView.View >
                    <GridView>
                        <GridViewColumn Header="ID" Width="60" DisplayMemberBinding="{Binding ID}"/>
                        <GridViewColumn Header="FloorName" Width="200" DisplayMemberBinding="{Binding FloorName}"/>
                    </GridView>
                </ListView.View>
            </ListView>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="6,37,0,0" Name="txtFloorName" VerticalAlignment="Top" Width="157" TextChanged="txtFloorName_TextChanged" />
            <Button Content="Remove" Height="23" HorizontalAlignment="Left" Margin="249,37,0,0" Name="btnRemoveFloor" VerticalAlignment="Top" Width="75" Click="btnRemoveFloor_Click" />
            <Label Content="Floor Name" Height="28" HorizontalAlignment="Left" Margin="6,6,0,0" Name="label1" VerticalAlignment="Top" />
        </Grid>
    </GroupBox>

关于按钮内容的If语句.WPF

按钮。内容类型为Object,使用"=="操作符将比较指针。你要做的是:

btnAddFloor.Content.ToString() = ="添加"