Set property '…Margin' threw an exception?

百般思念 提交于 2020-01-06 05:49:08

问题


I need some advice on how to fix this bug:

Exception:

"'Set property 'System.Windows.FrameworkElement.Margin' threw an exception.' Line number '6' and line position '31'."

InnerException:

"'12,10,599,Auto' is not a valid value for property 'Margin'."

I dont know how it could happen, because I didn't touch XAML yet. I used only designer.

Here is my XAML (There is no any "Auto" in properties ):

<Window x:Class="LinksGraph.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="774" Width="671" Closing="Window_Closing">
        <Grid>
            <Label Content="URL:" Height="28" Margin="12,10,599,0" Name="lURL" VerticalAlignment="Top" FontWeight="Bold" />
            <Button Content="Start" Height="23" Margin="0,44,93,0" Name="btnStart" VerticalAlignment="Top" HorizontalAlignment="Right" Width="75" Click="btnStart_Click" />
            <ProgressBar Height="10" HorizontalAlignment="Left" Margin="12,0,0,46" Name="pbStatus" VerticalAlignment="Bottom" Width="625" />
            <TextBox Height="23" Margin="56,10,107,0" Name="tbURL" VerticalAlignment="Top" />
        </Grid>
    </Window>

Thanks for any advice!


回答1:


You shouldn't specify control location with Margin, like you would do it with location in winForms. Instead let WPF arrange the controls automatically, like this:

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition />
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition />
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock Name="lURL" FontWeight="Bold" VerticalAlignment="Center">URL:</TextBlock>
            <TextBox Name="tbURL" Grid.Column="1" VerticalAlignment="Center" Margin="4"/>
            <Button Name="btnStart" Grid.Column="2" Margin="4" Content="Start" Padding="7,2,7,2"/>
            <ProgressBar Grid.Row="2" Height="15" Name="pbStatus" Grid.ColumnSpan="3" Margin="8"/>
        </Grid>

It would be much nicer and flexible than hard coding all control locations.




回答2:


Do you have any default styles setup for any of those controls that specifies a margin setting? They would look like:

<Style TargetType="{X:Type Button}>

</Style>

At this point the only thing I can figure is going on is that you have a custom default control style that is trying to use a resource in the margin property and the resource either is not the right type or it is not converting properly.




回答3:


I don't know really what happened, but only "format c:\" helped me solve this problem :( Nothing, including reinstal VS and all dependencies, didn't help... And that's sad :(



来源:https://stackoverflow.com/questions/6029987/set-property-margin-threw-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!