У меня есть следующий DataTemplate:Пользовательские свойства зависимостей не работает в шаблоне UWP с TemplatedParent
<DataTemplate x:Key="ListViewItemTemplate">
<Grid x:Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Margin="5"
Foreground="Black"
Text="{Binding ActualText}"
TextWrapping="WrapWholeWords" />
<my:CustomSelector Grid.Column="1"
ActualValue="{Binding Value, Mode=TwoWay,
RelativeSource={RelativeSource TemplatedParent}}" />
<ComboBox x:Name="cbox"
Grid.Column="2"
Width="90"
Margin="3"
VerticalAlignment="Center"
ItemsSource="{StaticResource data}"
SelectedIndex="{Binding Value,
Mode=TwoWay}" />
</Grid>
</DataTemplate>
Это его файл кода:
public sealed partial class CustomSelector : StackPanel
{
public int ActualValue
{
get { return (int)GetValue(ActualValueProperty); }
set { SetValue(ActualValueProperty, value); }
}
// Using a DependencyProperty as the backing store for ActualValue. This enables animation, styling, binding, etc...
public static readonly DependencyPropertyActualValueProperty =
DependencyProperty.Register("ActualValue", typeof(int), typeof(CustomSelector), new PropertyMetadata(0, (x, y) =>
{
CustomSelector cc = x as CustomSelector;
int content = (int)y.NewValue;
cc.ActualValue = content;
}));
public CustomSelector()
{
this.InitializeComponent();
DataContext = this;
}
}
Связывание в TextBlock
и ComboBox
хорошо работает поэтому DataContext правильный.
я узнал, что пользовательские привязки в шаблонах работать только если RelativeSource
установлен в TemplatedParent
.
К сожалению ActualValue
свойство моего пользовательского элемента управления всегда устанавливается в состояние по умолчанию (что явно не так), и когда я изменить значение, оно всегда начинается с 0.
Что еще я мог сделать?