[英]WPF DependencyProperty in ContentControl does not bind TwoWay
I have custom ContentControl
我有自定义
ContentControl
public class FilteringColumnHeader : ContentControl
{
public static readonly DependencyProperty TextFieldProperty =
DependencyProperty.Register("TextField", typeof(string), typeof(FilteringColumnHeader), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string TextField
{
get
{
return (string)GetValue(TextFieldProperty);
}
set { SetValue(TextFieldProperty, value); }
}
}
With this template style 使用此模板样式
<Style TargetType="{x:Type c:FilteringColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type c:FilteringColumnHeader}">
<DockPanel>
<ContentPresenter DockPanel.Dock="Top" Content="{TemplateBinding Content}" />
<TextBox Text="{TemplateBinding TextField}"/>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And this is how I use it in DataGrid
in XAML 这就是我在XAML中的
DataGrid
中使用它的方式
<DataGridTextColumn x:Name="NameColumn" Header="Name" Binding="{Binding Name}" Width="*" MinWidth="50">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<c:FilteringColumnHeader Content="{Binding }" Width="{Binding ActualWidth, ElementName=NameColumn}" TextField="{Binding DataContext.NameFilter, RelativeSource={RelativeSource AncestorType={x:Type local:GeneratorsListView}}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
And GeneratorsListView 和GeneratorsListView
private string nameFilter = "rec";
public string NameFilter
{
get { return nameFilter; }
set
{
nameFilter = value;
}
}
My problem is, the Text
binding works only OneWay. 我的问题是,
Text
绑定仅适用于OneWay。 When I run the code TextBox will be filled with "rec" and when I change the NameFilter
, the TextBox also changes. 当我运行代码时,TextBox将填充“ rec”,而当我更改
NameFilter
,TextBox也将更改。 But when I type something in that box, nothing happens (setter of NameFilter
is not being invoked at all). 但是,当我在该框中键入内容时,什么也没有发生(
NameFilter
没有调用NameFilter
设置)。 As you can see I've tried to set mode to TwoWay
everywhere I could, still nothing. 如您所见,我已尽我所能将模式设置为
TwoWay
,但仍然没有。 When I pleace regular TextBox
inside DataTemplate
and set the exact same Text binding, it is working. 当我在
DataTemplate
放入常规TextBox
并设置完全相同的Text绑定时,它正在工作。
{TemplateBinding}
is an optimized version of a binding with a mode of OneWay
so if you want the property to get updated you should use an ordinary binding with the RelativeSource
set to TemplatedParent
: {TemplateBinding}
是具有OneWay
模式的绑定的优化版本,因此,如果要更新属性,则应使用RelativeSource
设置为TemplatedParent
的普通绑定:
<ControlTemplate TargetType="{x:Type c:FilteringColumnHeader}">
<DockPanel>
<ContentPresenter DockPanel.Dock="Top" Content="{TemplateBinding Content}" />
<TextBox Text="{Binding TextField, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource TemplatedParent}}"/>
</DockPanel>
</ControlTemplate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.