简体   繁体   中英

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

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM