我知道当我写它时这是不对的,我已经能够为另一个答案收集大部分答案,但就是无法掌握最后一点。绑定确实从 UI 传递到 DependencyProperty(以及创建控件时的另一种方式)。
我的模板(需要将 IsChecked 绑定移动到实例):
<ControlTemplate x:Key="MyHeaderedContentTemplate" TargetType="HeaderedContentControl">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<ContentControl>
<StackPanel Orientation="Horizontal">
<CheckBox Margin="2,2,20,2"
Content="All/None"
IsChecked="{Binding Path=AllFeatureTypesChecked, Mode=TwoWay}" />
<TextBlock Text="{TemplateBinding Header}" Margin="2"/>
</StackPanel>
</ContentControl>
<ContentControl Grid.Row="1" Content="{TemplateBinding Content}" />
</Grid>
实例:
<HeaderedContentControl Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="2" Margin="4"
Template="{StaticResource ResourceKey=MyHeaderedContentTemplate}"
Header="Feature Details by Type">
<HeaderedContentControl.Resources>
</HeaderedContentControl.Resources>
<ListBox ItemTemplate="{StaticResource SelectableLinkNode}"
ItemsSource="{Binding Features}"/>
</HeaderedContentControl>
还有 Setter(当然,还有一个 Boolean AllFeatureTypesChecked DependencyProperty):
/// <summary>
/// Needs to be set on Startup and on ItemIsCheckedChanged Event from the Features List
/// </summary>
private void SetAllSelectedState()
{
bool allSelected = (Features.Count > 0);
foreach (var CheckableItem in Features) {
if (CheckableItem.IsChecked == false) {
allSelected = false;
break;
}
}
SetCurrentValue(AllFeatureTypesCheckProperty, allSelected);
}
供参考,这是DP
public static readonly DependencyProperty AllFeatureTypesCheckProperty =
DependencyProperty.Register("AllFeatureTypesCheck", typeof(Boolean), typeof(ReportSources),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAllFeatureTypesCheckedChanged));
这是非常有趣的东西,如果没有这里的很棒的人,我无法做到!谢谢!
更新:好的,现在我有了这个(头脑风暴):
<ControlTemplate x:Key="MyHeaderedContentTemplate" TargetType="HeaderedContentControl">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<ContentControl>
<StackPanel Orientation="Horizontal">
<ContentPresenter Content="{DynamicResource ResourceKey=CheckControl}" Margin="2,2,20,2"/>
<TextBlock Text="{TemplateBinding Header}" Margin="2"/>
</StackPanel>
</ContentControl>
<ContentControl Grid.Row="1" Content="{TemplateBinding Content}" />
</Grid>
</ControlTemplate>
像这样实例化:
<HeaderedContentControl Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" Grid.ColumnSpan="2" Margin="4"
Template="{StaticResource ResourceKey=MyHeaderedContentTemplate}"
Header="Feature Details by Type"
>
<HeaderedContentControl.Resources>
<CheckBox x:Key="CheckControl"
Content="All/None"
IsThreeState="True"
IsChecked="{Binding Path=AllFeatureTypesChecked, Mode=TwoWay}"
/>
</HeaderedContentControl.Resources>
<ListBox ItemTemplate="{StaticResource SelectableLinkNode}"
ItemsSource="{Binding Features}"/>
</HeaderedContentControl>
但在创建控件后仍然无法让 DP 上设置的值显示在 UI 上。
...当然可以在这里使用一些帮助,谢谢。