C# WPF:将属性绑定到样式中的对象
我有这种风格:C# WPF:将属性绑定到样式中的对象,c#,wpf,C#,Wpf,我有这种风格: <Style x:Key="{x:Type TextBox}" TargetType="TextBox"> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Border x:Name="border" BorderBrush="{Templ
<Style x:Key="{x:Type TextBox}" TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" CornerRadius="0,10,10,0" Padding="5,0,10,0" MinWidth="0" VerticalAlignment="Stretch">
<Grid VerticalAlignment="Center">
<Label x:Name="label" Content="{Binding LContent}" HorizontalAlignment="Left" VerticalAlignment="Stretch" Foreground="Gray" Padding="0,0,5,0" Margin="0" BorderBrush="#FF2C2C2C" BorderThickness="0,0,1,0"/>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Center">
<ScrollViewer x:Name="PART_ContentHost" Focusable="False" Template="{DynamicResource ComboBoxScrollViewerControlTemplate}" Margin="30,1,0,0" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" />
</Grid>
</Grid>
</Border>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="CaretBrush" Value="#FF646464"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="CaretBrush" Value="#FF323232"/>
</Trigger>
</Style.Triggers>
</Style>
但标签内容不会改变。
你能帮我吗?首先,我不确定
x:Key=“{x:Type TextBox}”
的作用,但要确保数据上下文设置正确。就像在窗口中使用它一样
你能行
<Window.DataContext>
<local:CustomizedTextBox></local:CustomizedTextBox>
</Window.DataContext>
虽然这不是一个好的做法
我还注意到,您已经将属性名称注册为LabelContent,并且在绑定中使用了LContent。将其更改为LabelContent会有所帮助
<Label x:Name="label" Content="{Binding LabelContent}" >
您已经将您的TargetType设置为TextBox,即使自定义控件是从TextBox派生的,它也不会应用于它,您也应该直接指定自定义控件类名,即CustomTextBox
为了清楚起见,我想说,如果您的样式中有一个x:Key属性,If将不会自动应用于所有targetType元素。然后必须显式指定每个控件的样式
为了让你离开,我做了这件事。如果你愿意,我会在稍后发布我所做的。但现在我很忙
希望它能有所帮助。您以与普通控件相同的方式处理模板绑定,这是错误的。可以这样想:如果要显式绑定到特定属性,那么定义模板完全是浪费时间。一个模板应该在一个控件的多个实例之间重用,并且它们不能全部绑定到一个属性,是吗 相反,您需要做的是使用:
{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Name}
或其简短版本:
{TemplateBinding Name}
这告诉绑定子系统使用父控件(您正在模板化的控件)作为绑定的源
对你来说可能是一个有价值的参考。前面的SO问题也有一个很好的简单示例。要直接从文本框文本设置标签内容,我们需要将标签内容设置为文本框的文本属性
<Label x:Name="label" "{Binding Text, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type TextBox}}}"
CustomizedTextBox是派生的TextBox吗?您正在将样式应用于自定义TextBox实例吗?INotifyPropertyChanged?CustomizedTextBox是一个用户控件,我没有使用INotifyPropertyChanged。您的绑定假定文本框的隐式
DataContext
是一个CustomizedTextBox
。因此,您的TextBox和CustomizedTextBox之间的关系还不清楚。你应该提供更多关于这方面的信息/背景。从技术上讲,Binding
无法解析源代码,因此您看不到标签的内容已填充。请注意,TemplateBinding
并不等同于Binding RelativeSource=…
。特别是,TemplateBinding
被设置为OneWay
。
<Label x:Name="label" "{Binding Text, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type TextBox}}}"
<TextBox Grid.Row="0" x:Name="CustomizedTextBox" Text="{Binding ElementName=MyMainWindow, Path=LabelContent }"
public static readonly DependencyProperty LContentProperty =
DependencyProperty.Register("LabelContent", typeof(string), typeof(MainWindow));
public string LabelContent
{
get { return (string)GetValue(LContentProperty); }
set { SetValue(LContentProperty, value); }
}