如何绑定到 WPF 中用户控件内的控件?
How to bind to a control inside a usercontrol in WPF?
我有以下用户控件..
`<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>
`
其背后的代码是..
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;
}
}
我正在尝试将 SearchText 属性 绑定到我使用用户控件的主页中的另一个控件
<local:SearchBox x:Name="SearchBox"/>
<TextBlock Text="{Binding ElementName=SearchBox,Path=SearchText,Mode=TwoWay}"/>
我想更新 texblock(或任何其他控件)的文本 属性,因为我的用户控件的 SearchText 发生了变化,但它不起作用...但是当我单击任何元素时,例如我的用户控件文本块文本中的按钮更新我不明白为什么会这样。
像这样将 UpdateSourceTrigger=PropertyChanged
添加到您的 UserControl 的文本框。
<TextBox
Grid.Column="1"
Tag="Search..."
Text="{Binding SearchText,
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type UserControl}},
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
我有以下用户控件..
`<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>
`
其背后的代码是..
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;
}
}
我正在尝试将 SearchText 属性 绑定到我使用用户控件的主页中的另一个控件
<local:SearchBox x:Name="SearchBox"/>
<TextBlock Text="{Binding ElementName=SearchBox,Path=SearchText,Mode=TwoWay}"/>
我想更新 texblock(或任何其他控件)的文本 属性,因为我的用户控件的 SearchText 发生了变化,但它不起作用...但是当我单击任何元素时,例如我的用户控件文本块文本中的按钮更新我不明白为什么会这样。
像这样将 UpdateSourceTrigger=PropertyChanged
添加到您的 UserControl 的文本框。
<TextBox
Grid.Column="1"
Tag="Search..."
Text="{Binding SearchText,
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type UserControl}},
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />