简体   繁体   中英

Button doesn't appear on window

I have a ErrorControl Class that handles a text being sent to this control, and displays it in a window. Along with the text in the window, I have a btnOk that should display. So a window with just a label and a button. The label with the error displays fine, but the button does not show.

code behind:

        public void showErrorMessage(String error, String title)
    {
        ErrorControl errorMessage = new ErrorControl();
        errorMessage.errStack.Children.Remove(errorMessage.lblError);
        errorMessage.lblError.Content = error.ToString();
        errorMessage.errStack.Children.Add(errorMessage.lblError);
        Window wind = new Window()
        {
            Content = error,
            Title = title,
            SizeToContent = SizeToContent.WidthAndHeight,
            ResizeMode = ResizeMode.NoResize,
            WindowStyle = WindowStyle.ToolWindow
        };

        wind.ShowDialog();
    }

xaml:

    <Grid Name="Main">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
    <StackPanel Name="errStack">
        <Label Name="lblError" Content="{Binding Path=ErrorMessage}" />
    </StackPanel>
    <Button Name="btnOk" Content="Ok" Grid.Row="1" HorizontalAlignment="Center" MinWidth="75" VerticalAlignment="Center" Click="btnOk_Click"/>

</Grid>

</Grid>

and this is how I am sending the error

                    String error = "myerror";
                ErrorControl errorControl = new ErrorControl();
                errorControl.showErrorMessage(error, "Error Message");

why is my button not appearing in my window? it is defined in my xaml.

Your problem is that you assign the error text instead the ErrorControl to Window.Content .

Corrected:

Window wind = new Window()
{
    Content = errorMessage, // << your problem is here
    Title = title,
    SizeToContent = SizeToContent.WidthAndHeight,
    ResizeMode = ResizeMode.NoResize,
    WindowStyle = WindowStyle.ToolWindow
};

Also I want to add that your code has design problems. showErrorMessage should be at least static so you don't have to create an ErrorControl and just call

ErrorControl.showErrorMessage(error, "Error Message");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM