简体   繁体   English

如何绑定到WPF中的usercontrol内的控件?

[英]How to bind to a control inside a usercontrol in WPF?

I have the following user control.. 我有以下用户控件..

`<UserControl x:Class="School_Manager.SearchBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:School_Manager"
             mc:Ignorable="d"
             d:DesignHeight="50" d:DesignWidth="400">


        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <!--Cancle Button-->
            <Button Grid.Column="0"
                    Margin="0 0 -10 0"
                    Style="{StaticResource IconGrowButton}"
                    Content="{StaticResource FontAwesomeCloseIcon}"
                    />

            <!--SearchBox-->
            <TextBox 
                Grid.Column="1"
                Style="{StaticResource SearchTextBox}"
                Tag="Search..."
                Text="{Binding SearchText,
                       RelativeSource={RelativeSource 
                       Mode=FindAncestor, 
                       AncestorType={x:Type UserControl}}, 
                       Mode=TwoWay}" />

            <!--Search Button-->
        <Button Grid.Column="2"
                    Margin="-10 0 0 0"
                    Style="{StaticResource IconGrowButton}"
                    Content="{StaticResource FontAwesomeSearchIcon}"
                    />

        </Grid>
</UserControl>
`

and its code behind is.. 它背后的代码是......

 public partial class SearchBox : UserControl
{

    public SearchBox()
    {
        InitializeComponent();
    }



    public string SearchText
    {
        get => (string)GetValue(SearchTextProperty);
        set => SetValue(SearchTextProperty, value);
    }
    public static readonly DependencyProperty SearchTextProperty =
      DependencyProperty.Register("SearchText", typeof(string), typeof(SearchBox), new PropertyMetadata(new PropertyChangedCallback(OnSearchTextChanged)));


    private static void OnSearchTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var searchBox = (SearchBox)d;
        searchBox.SearchText = e.NewValue as string;
    }


}

i am trying to bind the SearchText property to another control in my main page where i am using the user control 我试图将SearchText属性绑定到我的主页面中我使用用户控件的另一个控件

 <local:SearchBox x:Name="SearchBox"/>

 <TextBlock Text="{Binding ElementName=SearchBox,Path=SearchText,Mode=TwoWay}"/>

i want to update the texblock's (or any other control's) text property as the SearchText of my usercontrol changes but its not working... but when i click any element like button in my usercontrol textblock's text updates i don't get it why its happening. 我想更新texblock(或任何其他控件)的文本属性,因为我的usercontrol的SearchText发生了变化,但它不起作用......但是当我在usercontrol textblock的文本更新中点击任何元素之类的按钮时,我不知道为什么它发生。

Add UpdateSourceTrigger=PropertyChanged to your UserControl's TextBox like this. 像这样将UpdateSourceTrigger=PropertyChanged添加到UserControl的TextBox中。

 <TextBox 
    Grid.Column="1"
    Tag="Search..."
    Text="{Binding SearchText,
           RelativeSource={RelativeSource 
           Mode=FindAncestor, 
           AncestorType={x:Type UserControl}}, 
           Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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