你是对的,由于某种原因,以下方法不起作用,尽管相同的绑定工作得很好,你将它用于例如Text
a 的属性TextBox
:
<Button Style="{StaticResource SkipBackAppBarButtonStyle}" AutomationProperties.Name="{Binding SelectedItem, ElementName=List}" />
我确实设法通过在视图模型中使用属性并绑定到它来使其工作,ListView.SelectedItem
并且AutomationProperties.Name
:
<ListView ItemsSource="{Binding Strings}"
SelectedItem="{Binding SelectedString, Mode=TwoWay}" />
<!-- ... -->
<Button Style="{StaticResource SkipBackAppBarButtonStyle}"
AutomationProperties.Name="{Binding SelectedString}" />
SelectedString
应该是视图模型中的一个属性,实现INotifyPropertyChanged
:
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
Strings = new ObservableCollection<string>();
for (int i = 0; i < 50; i++)
{
Strings.Add("Value " + i);
}
}
public ObservableCollection<string> Strings { get; set; }
private string _selectedString;
public string SelectedString
{
get { return _selectedString; }
set
{
if (value == _selectedString) return;
_selectedString = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}