用户控件依赖项 属性 不起作用
UserControl Dependency property does not work
我有我的用户控件。我想设置依赖关系 属性,但我不能。
TextBoxExtended.cs
public partial class TextBoxExtended : UserControl
{
public static readonly DependencyProperty MyTextPropertyProperty =
DependencyProperty.Register("MyTextProperty", typeof(string), typeof(TextBoxExtended), new UIPropertyMetadata(String.Empty));
public string MyTextProperty
{
get { return (string)GetValue(MyTextPropertyProperty); }
set { SetValue(MyTextPropertyProperty, value); }
}
TextBoxExtended.xaml
<TextBox x:Name="tbMain" Text="{Binding MyTextProperty}"
MainWindow.xaml
<local:TextBoxExtended x:Name="TextBoxSearch" Height="30" Margin="83,38,193,285" MyTextProperty="MyTextProperty"/>
我需要事件文本更改,我使用此代码
<local:TextBoxExtended x:Name="TextBoxSearch" Height="30" Margin="83,38,193,285" MyTextProperty="{Binding MyText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
但我需要在引入每个字符后进行更改
您必须在 TextBoxExtended.xaml 中设置绑定的源对象,例如
<TextBox x:Name="tbMain"
Text="{Binding MyTextProperty,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
在 TextBox 的绑定中使用 ElementName。
<StackPanel>
<TextBox Text="{Binding MyText, ElementName=TextBoxSearch}" ></TextBox>
<local:TextBoxExtended x:Name="TextBoxSearch" Height="30"
MyText="Hello World!!!" />
</StackPanel>
我有我的用户控件。我想设置依赖关系 属性,但我不能。
TextBoxExtended.cs
public partial class TextBoxExtended : UserControl
{
public static readonly DependencyProperty MyTextPropertyProperty =
DependencyProperty.Register("MyTextProperty", typeof(string), typeof(TextBoxExtended), new UIPropertyMetadata(String.Empty));
public string MyTextProperty
{
get { return (string)GetValue(MyTextPropertyProperty); }
set { SetValue(MyTextPropertyProperty, value); }
}
TextBoxExtended.xaml
<TextBox x:Name="tbMain" Text="{Binding MyTextProperty}"
MainWindow.xaml
<local:TextBoxExtended x:Name="TextBoxSearch" Height="30" Margin="83,38,193,285" MyTextProperty="MyTextProperty"/>
我需要事件文本更改,我使用此代码
<local:TextBoxExtended x:Name="TextBoxSearch" Height="30" Margin="83,38,193,285" MyTextProperty="{Binding MyText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
但我需要在引入每个字符后进行更改
您必须在 TextBoxExtended.xaml 中设置绑定的源对象,例如
<TextBox x:Name="tbMain"
Text="{Binding MyTextProperty,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
在 TextBox 的绑定中使用 ElementName。
<StackPanel>
<TextBox Text="{Binding MyText, ElementName=TextBoxSearch}" ></TextBox>
<local:TextBoxExtended x:Name="TextBoxSearch" Height="30"
MyText="Hello World!!!" />
</StackPanel>