2016-10-11 4 views
1

Я хочу привязать данные к сетке в xaml. Это код, я использую для его связывания:WPF Databound Grid Scaling

<Grid x:Name="Example" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > 
     <ContentPresenter Content="{Binding ExampleImage}" /> 
    </Grid> 

Теперь, когда я связываю его с помощью фиксированной ширины и высоты он будет отображать сетку в заданных размерах.

Код:

private Grid _exampleImage; 

    public Grid ExampleImage 
    { 
     get 
     { 
      if (SelectedSectie != null) 
      { 
       var convertFromString = System.Windows.Media.ColorConverter.ConvertFromString("#CCCCCC"); 
       if (convertFromString != null) 
       { 
        DropShadowEffect dse = new DropShadowEffect 
        { 
         BlurRadius = 5, 
         ShadowDepth = 1, 
         Direction = 270, 
         Color = 
          (System.Windows.Media.Color) 
          convertFromString 
        }; 

        _exampleImage = new Grid 
        { 
         Background = 
          new SolidColorBrush(SingleIcons.Helpers.ColorConverter.ToMediaColor(SelectedSectie.Color.ColorValue)), 
         VerticalAlignment = VerticalAlignment.Stretch, 
         Width = SelectedIconSize.Width, 
         Height = SelectedIconSize.Height, 
         MaxWidth = SelectedIconSize.Width, 
         MaxHeight = SelectedIconSize.Height, 
         HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, 
         Effect = dse, 
         RowDefinitions = 
         { 
          new RowDefinition {Height = new GridLength(46, GridUnitType.Star)}, 
          new RowDefinition {Height = new GridLength(3, GridUnitType.Star)} 
         } 
        }; 
       } 

       TextBlock afbeeldingTextBlock = new TextBlock 
       { 
        Text = _selectedSectie.Sectie, 
        TextWrapping = TextWrapping.Wrap, 
        TextAlignment = TextAlignment.Center, 
        Width = _exampleImage.Width, 
        FontSize = Global.Fontsize, 
        FontFamily = new System.Windows.Media.FontFamily(Global.Family.Name), 
        VerticalAlignment = VerticalAlignment.Center, 
        Foreground = 
         new SolidColorBrush(
          SingleIcons.Helpers.ColorConverter.ToMediaColor(SelectedSectie.Color.TextColorValue)), 

       }; 

       TextOptions.SetTextFormattingMode(afbeeldingTextBlock, TextFormattingMode.Display); 
       TextOptions.SetTextRenderingMode(afbeeldingTextBlock, TextRenderingMode.ClearType); 

       Canvas bottomCanvas = new Canvas 
       { 
        Background = (SolidColorBrush) (new BrushConverter().ConvertFrom("#26000000")) 
       }; 

       Grid.SetRow(afbeeldingTextBlock, 0); 
       Grid.SetRow(bottomCanvas, 1); 

       _exampleImage.Children.Add(afbeeldingTextBlock); 
       _exampleImage.Children.Add(bottomCanvas); 

       _exampleImage.Measure(new System.Windows.Size(_exampleImage.Width, 
        _exampleImage.Height)); 
       _exampleImage.Arrange(
        new Rect(new System.Windows.Size(_exampleImage.Width, _exampleImage.Height))); 


       return _exampleImage; 
      } 
      else 
      { 
       return null; 
      } 

     } 
    } 

Теперь то, что я хотел бы сделать, это держать сетки в заданных размерах, но позволяет GUI масштабировать сетку, , потому что, когда я сделать окно меньше сетки остается на них размеров, и это вредит масштабированию приложения.

Причина, по которой я использую сетку привязки данных, состоит в том, что у меня есть другая функция, которая экспортирует эту сетку в .png.

ответ

0

Ввод его в поле просмотра и установка StretchDirection to DownOnly - результат, который я хотел.

<Grid x:Name="Example" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > 
     <Viewbox StretchDirection="DownOnly" > 
      <ContentPresenter Content="{Binding ExampleImage}" /> 
     </Viewbox> 
</Grid>