开发者

Setting Viewbox.Child causes ArgumentException

开发者 https://www.devze.com 2023-01-22 18:38 出处:网络
The exception message is \"Value does not fall within the expected range\". Here\'s the code: private void PrintButton_Click(object sender, RoutedEventArgs e)

The exception message is "Value does not fall within the expected range". Here's the code:

private void PrintButton_Click(object sender, RoutedEventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += (s, pa) =>
        {
            Viewbox vb = new Viewbox();
            vb.Child = MainGrid; // MainGrid is a System.Windows.Control开发者_如何学Cs.Grid.
            pa.PageVisual = vb;
        };

    pd.Print("Test Page");
}

Am I doing something wrong here?


The "Value does not fall within expected range" error occurs when you try to set a UIElement that already has a parent as the child of a container, when you try to add two instances of a UIElement with the same "name property" to a parent container, etc. In your example, MainGrid must be the child of another container, and therefore cannot be added as a child of the Viewbox. Remove MainGrid from it's parent, then add it to the Viewbox. When you're done remove MainGrid from the Viewbox and add it back to it's original parent.

0

精彩评论

暂无评论...
验证码 换一张
取 消