简体   繁体   English

按钮不会出现在窗口上

[英]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. 我有一个ErrorControl类来处理发送到此控件的文本,并将其显示在窗口中。 Along with the text in the window, I have a btnOk that should display. 随着窗口中的文本,我有一个应该显示的btnOk。 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: 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. 它在我的xaml中定义。

Your problem is that you assign the error text instead the ErrorControl to Window.Content . 您的问题是您将错误文本而不是ErrorControl分配给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 showErrorMessage至少应该是静态的,因此您不必创建ErrorControl并只需调用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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