Ответ 1
В WinRT RelativeSourceMode поддерживает только Self и TemplatedParent, FindAncestor недоступен. Поэтому, когда вы используете XAML Behaviors, вам нужно использовать ElementName
как обходной путь. И если вы используете DataContext или ViewModel в своем проекте, вы можете привязываться к DataContext или ViewModel, чтобы избежать использования ElementName
. Например:
<Page ...>
<Page.Resources>
<local:MyViewModel x:Key="ViewModel" />
</Page.Resources>
...
<Border x:Name="BackgroundElement" DataContext="{Binding Source={StaticResource ViewModel}}">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding Tag}" Value="Text">
<Core:ChangePropertyAction PropertyName="Background" Value="Red" />
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Border>
...
</Page>
И ViewModel, используемый выше:
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _tag;
public string Tag
{
get
{
return _tag;
}
set
{
_tag = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Tag"));
}
}
}
}