1

I have a window, in which a have a rectangle. I want its fill to be a gradientstop for another rectangle fill, but on different window. The "source" window is defined as:

<Window x:Class="WPF1.ColorSelectorWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:WPF1"
    x:Name="colorSelectorWindow"
    Title="ColorSelectorWindow" Height="281" Width="540">

There's a rectangle

    <Rectangle HorizontalAlignment="Left" Margin="40,120,0,41" Stroke="Black" Width="100" Name="ColorPicker">
        <Rectangle.Fill>
            <SolidColorBrush>
                <SolidColorBrush.Color>
                    ...
                </SolidColorBrush.Color>
            </SolidColorBrush>
        </Rectangle.Fill>
    </Rectangle>

and a property:

public partial class ColorSelectorWindow : Window
{
    public Brush SelectedBrush
    {
        get
        {
            return ColorPicker.Fill;
        }
    }
}

Now, in the target window, I define a rectangle:

    <Rectangle Height="213" HorizontalAlignment="Left" Margin="27,8,0,0" Name="rectangle1" VerticalAlignment="Top" Width="25" Grid.Row="1">
        <Rectangle.Fill>
            <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                <GradientStop Offset="0" Color="Blue" />
                <GradientStop Offset="1" Color="{Binding ElementName=colorWindowSelector, Path=SelectedBrush, Converter={StaticResource BrushToColorConverter}}" />
            </LinearGradientBrush>
        </Rectangle.Fill>
    </Rectangle>

where BrushToColorConverter converts Brush to Color. This isn't working and I don't know why, it seems I have a problem with addressing that "source" window.. I'm building it in the target window constructor:

        public MainWindow()
        {
            colorWindow = new ColorSelectorWindow();
            colorWindow.Name = "colorWindowSelector";
            colorWindow.Hide();
            InitializeComponent();
        }

I'm only hiding and showing it, not closing for sure. How to bind it properly?

deha
  • 805
  • 8
  • 29
  • Use bindin to `OwnerWindow` property... http://stackoverflow.com/questions/7825379/handling-a-bubbling-event-raised-from-a-modal-dialog-in-main-window/7831973#7831973 – WPF-it Oct 28 '11 at 07:04

1 Answers1

1

You need to have property exposed SelectedBrush on your MainWindow and you just need to bind to the color of this Brush using RelativeSource. So, this will do for you -

<Rectangle Height="213" HorizontalAlignment="Left" Margin="27,8,0,0" Name="rectangle1" VerticalAlignment="Top" Width="25" Grid.Row="1">
   <Rectangle.Fill>
     <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
        <GradientStop Offset="0" Color="Blue" />
        <GradientStop Offset="1" Color="{Binding Path=Owner.SelectedBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType =Window} Converter={StaticResource BrushToColorConverter}}" />
     </LinearGradientBrush>
   </Rectangle.Fill>
</Rectangle>

But make sure while creating the window, you set its owner to be MainWindow -

public MainWindow()
{
   InitializeComponent();
   colorWindow = new ColorSelectorWindow();
   colorWindow.Name = "colorWindowSelector";
   colorWindow.Owner = this;
   colorWindow.Hide();
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • I need to have that property on the other window – deha Oct 27 '11 at 20:01
  • If you have property in the same window where your rectangle is then simply replace the Binding path to say SelectedBrush in place of Owner.SelectedBrush.. No need of setting Owner then.. :) – Rohit Vats Oct 27 '11 at 20:06
  • yes, but SelectedBrush is in the Owned window, not in the Owning one. From target (MainWindow) I need to address ColorSelectorWindow.SelectedBrush. Your solution seems to be looking for MainWindow.SelectedBrush and binding it to ColorSelectorWindow rectangle fill which is not what I'm looking for... Or I misunderstood sth – deha Oct 27 '11 at 20:10
  • Ok, you want your child window to pass on property to your Main Window. In case you are implementing MVVM your answer lies here - http://stackoverflow.com/questions/7507527/passing-value-from-child-window-to-parent-window-using-wpf-and-mvvm-pattern – Rohit Vats Oct 27 '11 at 20:21
  • Otherwise if not following MVVM i would suggest you to put that property in base class and inherit both windows from that base class so that both can access that property. Let me know in case f any issues. – Rohit Vats Oct 27 '11 at 20:22