deha
deha

Reputation: 815

How bind control property to a another window property?

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?

Upvotes: 1

Views: 892

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81253

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();
}

Upvotes: 1

Related Questions