-1

I have the following Viewbox in MainWindow.Xaml:

    <Viewbox x:Name="R1C1Viewbox" Grid.Row="1" Grid.Column="2"
                                   Grid.ColumnSpan="1" Grid.RowSpan="1">
      <Border x:Name="R1C1Border" BorderBrush="White" CornerRadius="3"
                                  BorderThickness="2" Background="White"  
                                  Height="40" Width="40" Margin="0,3,3,0">
        <Grid x:Name="R1C1Grid">
          <TextBlock x:Name="R1C1TextBlock1" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Arial" FontWeight="Bold" Text="+" FontSize="22"></TextBlock>
          <Polygon x:Name="R1C1LED"
              Points="0,0 15,0 0,15"
              Stroke="#FFED1C24" 
              StrokeThickness="1">
            <Polygon.Fill>
              <RadialGradientBrush>
                <GradientStop Color="White" Offset="2.5"/>
                <GradientStop Color="Black"/>
              </RadialGradientBrush>
            </Polygon.Fill>
          </Polygon>
          <Button x:Name="R1C1Button" Background="Transparent"
                  BorderBrush="Transparent">
          </Button>
        </Grid>
      </Border>
    </Viewbox>

In my code behind, I define 2 RadialGradientBrushes for the Polygon.Fill property:

public static RadialGradientBrush ledOn = new RadialGradientBrush();
public static RadialGradientBrush ledOff = new RadialGradientBrush();


  ledOn.GradientStops.Add(new GradientStop(Colors.Red, .8));
  ledOn.GradientStops.Add(new GradientStop(Colors.White, 0));
  ledOff.GradientStops.Add(new GradientStop(Colors.White, 2.5));
  ledOff.GradientStops.Add(new GradientStop(Colors.Black, 0));

If I load the viewbox only from MainWindow.xaml, I can use:

    R1C1LED.Fill = ledOn;

or

R1C1LED.Fill = ledOff;

and the fill changes as expected. If I load the exact same xaml from a file using XamlReader(), the Viewbox displays exactly as expected but using the code behind to change the fill as above doesn't change the fill and no errors are generated.

user294382
  • 1,709
  • 3
  • 10
  • 8

1 Answers1

0

Example code:

private void OnTest(object sender, RoutedEventArgs e)
{
    var xaml =
        @"<Window 
             xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
             xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
             xmlns:d='http://schemas.microsoft.com/expression/blend/2008'
             xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
             mc:Ignorable='d'
             Title='WPF App' Height='450' Width='800'>

            <StackPanel Orientation='Vertical'>
                <Button x:Name='R1C1LED' Content='Load Data' />
            </StackPanel>
        </Window>";


    var win = (Window)XamlReader.Parse(xaml);
    var button = (Button)win.FindName("R1C1LED");
    button.Background = Brushes.Red;
    button.Click += (obj, args) => { MessageBox.Show("Hi!"); };
    win.Show();

}
JohnyL
  • 6,894
  • 3
  • 22
  • 41
  • It exists, it just doesn't change. As I said, no errors are generated. If I had tried to assign a value to a null object it would have generated an error. If I use XamlWriter.Save(R1C1LED) generated is the same whether I use FindName or not. The Fill property just doesn't change with R1C1LED.Fill = ledOn. – user294382 Dec 23 '17 at 17:22
  • I changed answer to show you example what you need to to. Note the absence of `x:Class` attribute. – JohnyL Dec 23 '17 at 17:54
  • Thanks, I'll give that a try and let you know how it works out. Remember though, my Window already exists as do the elements. I'm just loading new values/properties to each element(viewbox) using Grid.Children.Add(element). – user294382 Dec 23 '17 at 20:05