0

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();
    }
}
  • Give your UserControl an x:Name="root" and then {Binding whatever, ElementName=root} –  Mar 30 '15 at 17:00
  • Can you clarify what you mean when you say your binding isn't working? I copy/pasted your code into a sample WPF project and the binding works just fine for me. My best guess would be that your `PopupContent` property works on a different layer of the visual tree than most controls, so it doesn't have the same UI tree to traverse as everything else. You could use a tool like [Snoop](https://snoopwpf.codeplex.com/) to verify the visual tree is as you expect it, and the CheckBox is somewhere within the SolutionViewContext visual tree. – Rachel Mar 30 '15 at 17:49
  • Welcome to Stack Overflow! I fixed some grammar problems with your question. You might consider changing your question title to be a bit more specific. – Conrad Frix Mar 30 '15 at 20:12
  • **Rachel** you are right. CheckBox does not exist in visual tree. It is nowhere. What can I do else? – andrei.aliashkevich Mar 31 '15 at 08:35
  • @andrei.aliashkevich Glad to see you got it sorted out :) – Rachel Mar 31 '15 at 12:44

2 Answers2

1

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

Community
  • 1
  • 1
0

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>
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • **Will**, **Peter Duniho** I also tried this case, and it also does not work. – andrei.aliashkevich Mar 30 '15 at 17:45
  • @andrei.aliashkevich: then you will need to provide [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) that reliably demonstrates the problem. Also, be more specific than "it also does not work". – Peter Duniho Mar 30 '15 at 17:48