WPF自定义控件绑定

时间:2015-03-30 16:57:52

标签: c# wpf binding controls

我编写了自定义控件SearchTextBox。此控件具有属性PopupContent。 PopupContent有一个CheckBox,我想将它绑定到属性IsChecked但绑定不起作用。我该怎么做才能正确?

<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>

代码背后:

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

2 个答案:

答案 0 :(得分:1)

问题已解决。 Popup就像ContextMenu,ToolTip控件,它们没有被添加到VisualTree中。回答here

答案 1 :(得分:0)

正如威尔说,您可以通过为SolutionViewContent对象提供名称,然后在绑定中引用该名称来实现此目的。

例如:

<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>