andrei.aliashkevich
andrei.aliashkevich

Reputation: 93

WPF custom control binding

I wrote my custom control SearchTextBox. This control has the property PopupContent. PopupContent has a CheckBox and I want to bind it to the property IsChecked But the binding does not work. How can I do this correctly?

<UserControl x:Class="TestEnv2.PanelViews.SolutionView.SolutionViewContent">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="2"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Border Grid.Row="0" x:Name="SearchPanel" Visibility="Hidden" Background="#efeff2" >

            <ctrl:SearchTextBox x:Name="SearchControl" Height="21" BorderThickness="0" VerticalContentAlignment="Center" Background="White"
                                SearchMode="Delayed" LabelText="Search Solution Explorer" Search="SolutionView_Search">

                <ctrl:SearchTextBox.PopupContent>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="25"/>
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <TextBlock Grid.Row="0" Margin="5,0,0,0" Text="Search options" Foreground="Gray" VerticalAlignment="Center"/>

                        <CheckBox Grid.Row="1" Margin="5,0,0,5" Content="Match case"
                                  IsChecked="{Binding Path=SearchMatchCase, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type panels_soln:SolutionViewContent}}}"/>
                    </Grid>
                </ctrl:SearchTextBox.PopupContent>
            </ctrl:SearchTextBox>
        </Border>
    </Grid>
</UserControl>

Code behind:

public partial class SolutionViewContent : UserControl
{
    public static readonly DependencyProperty SearchMatchCaseProperty = DependencyProperty.
        Register("SearchMatchCase", typeof(Boolean), typeof(SolutionViewContent), new UIPropertyMetadata(true));

    public Boolean SearchMatchCase
    {
        get { return (Boolean)GetValue(SearchMatchCaseProperty); }
        set
        {
            MessageBox.Show("SearchMatchCase");
            SetValue(SearchMatchCaseProperty, value);
        }
    }

    public SolutionViewContent()
    {
        InitializeComponent();
    }
}

Upvotes: 0

Views: 140

Answers (2)

andrei.aliashkevich
andrei.aliashkevich

Reputation: 93

Problem resolved. Popup is like ContextMenu, ToolTip controls, They are not added to the VisualTree. Answer here.

Upvotes: 1

Peter Duniho
Peter Duniho

Reputation: 70652

As commenter Will says, you can do this by giving your SolutionViewContent object a name, and then referencing that name in your binding.

For example:

<UserControl x:Class="TestEnv2.PanelViews.SolutionView.SolutionViewContent"
             <!-- any name here will do...you just have to make sure to 
                  use the same name in the binding -->
             x:Name="solutionViewContent1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="2"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Border Grid.Row="0" x:Name="SearchPanel" Visibility="Hidden" Background="#efeff2" >

            <ctrl:SearchTextBox x:Name="SearchControl" Height="21" BorderThickness="0" VerticalContentAlignment="Center" Background="White"
                                SearchMode="Delayed" LabelText="Search Solution Explorer" Search="SolutionView_Search">

                <ctrl:SearchTextBox.PopupContent>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="25"/>
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <TextBlock Grid.Row="0" Margin="5,0,0,0" Text="Search options" Foreground="Gray" VerticalAlignment="Center"/>

                        <CheckBox Grid.Row="1" Margin="5,0,0,5" Content="Match case"
                                  IsChecked="{Binding ElementName=solutionViewContent1, Path=SearchMatchCase}"/>
                    </Grid>
                </ctrl:SearchTextBox.PopupContent>
            </ctrl:SearchTextBox>
        </Border>
    </Grid>
</UserControl>

Upvotes: 0

Related Questions