2013-01-08 29 views
2

我有一個應用欄,像這樣Windows應用商店的應用的應用欄按鈕文本裝訂

<Page.BottomAppBar> 
    <AppBar x:Name="bottomAppBar" Padding="10,10,10,10" > 
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> 
      <Button Style="{StaticResource ReadAppBarButtonStyle}" > 
      </Button> 
     </StackPanel> 
    </AppBar> 
</Page.BottomAppBar> 

一些按鈕我想按鈕上的文字爲的ListView的選擇項屬性綁定,並使用的IValueConverter

我發現,按鈕上的文字是用AutomationProperties.Name

知道怎樣才能XAML代碼綁定這個屬性進行設置。

感謝

+0

那麼問題是什麼?只需將綁定設置爲AutomationProperties.Name =「{綁定SelectedItem,ElementName =列表}」 – Nagg

+0

我這樣做了但文本顯示爲空 –

+0

如何以編程方式更改AutomationProperties.Name? –

回答

3

你是對的,由於某種原因,以下是不行的,雖然同樣綁定工作就好了,你用它例如一個TextBoxText屬性:

<Button Style="{StaticResource SkipBackAppBarButtonStyle}" AutomationProperties.Name="{Binding SelectedItem, ElementName=List}" /> 

我還是設法得到它通過在視圖模型使用屬性,並結合了這兩個工作ListView.SelectedItemAutomationProperties.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)); 
    } 
} 
+0

謝謝,我選擇了另一個解決方案,即檢查AppBar_Opened事件處理程序中選中的項目列表 –