你是對的,由於某種原因,以下是不行的,雖然同樣綁定工作就好了,你用它例如一個TextBox
的Text
屬性:
<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));
}
}
那麼問題是什麼?只需將綁定設置爲AutomationProperties.Name =「{綁定SelectedItem,ElementName =列表}」 – Nagg
我這樣做了但文本顯示爲空 –
如何以編程方式更改AutomationProperties.Name? –