2013-10-05 33 views
1

我找不到任何例子,讓我明白如何以及如果我可以更改C#中的數據綁定點擊一個按鈕,在我的情況下切換開關,基本上我有32個按鈕在我的應用程序和32個按鈕的行爲相同但需要使用不同的文本(取決於它們當前數據綁定的某些切換開關),以便可以從本地存儲器保存和檢索文本,但取決於這些切換開關的狀態。我可以將按鈕內容數據綁定交換到代碼中的其他數據綁定嗎?

所以我目前有:

<Button x:Name="_ovButton1" Content="{Binding Source={StaticResource AppSettings}, Path=ovName1_1Value, Mode=TwoWay}" Margin="2,0,250,0" VerticalAlignment="Top" FontSize="14" Height="72" FontWeight="Bold" MouseLeftButtonUp="_ovButton1_MouseLeftButtonUp" MouseLeftButtonDown="_ovButton1_MouseLeftButtonDown" ClickMode="Hover" Hold="_ovButton1_Hold"/> 

,我想,當用戶更改toggleswitch的狀態到

{StaticResource AppSettings}, Path=ovName1_1Value, Mode=TwoWay} 

改變,例如:

{StaticResource AppSettings}, Path=ovName1_2Value, Mode=TwoWay} 

但我找不到任何示例顯示如何在c#中執行此操作。 需要執行哪些代碼是什麼?

+0

執行按鈕執行基於文本不同的動作?換句話說,按鈕1在點擊時總是做'X'嗎?或者當文本不同時它會「Y」? –

+0

不,讓我澄清...這是總體目標:我有32個按鈕,按下它們時的動作是相同的,不管怎樣,但我希望標籤根據所選的切換開關進行更改(只有一個一個時間)http://www.ctwo12.com/fsr/fsr5.png這是我可以讓用戶保存標籤,讓他們知道他們點擊了什麼......正如你可以在屏幕截圖中看到你有6個切換和很多按鈕我想要相同的按鈕來說,例如在第一個按鈕button1 page1上,然後當用戶點擊下一個切換按鈕時,page1將變成button1 page2 ......這有意義嗎? –

+0

因此,32個按鈕,每個都執行不同的操作,但不管顯示哪個文本,按鈕1總是會執行'X'操作。爲什麼文本改變總是會出現'X'? –

回答

0

可以在這樣的代碼指定綁定的目標:

MyData myDataObject = new MyData(DateTime.Now);  
Binding myBinding = new Binding("MyDataProperty"); 
myBinding.Source = myDataObject; 
myText.SetBinding(TextBlock.TextProperty, myBinding); 

多見於http://msdn.microsoft.com/en-us/library/ms742863.aspx

+0

我已經看到了,但是在我上面的xaml代碼中(我知道我對此感到非常愚蠢),但是代碼中的哪一部分將路徑部分放在一邊,並且資源部分是否會去? –

0

- 編輯注我沒有獲得一個WP8模擬器來測試這一點 - -

在視圖模型,它看起來像這樣:

public List<string> Members 
{ 
    get { return _Members; } 
    set { _Members = value; OnPropertyChanged(); } 
} 
public MainVM() 
{ 
    // Simulate Asychronous access, such as to a db. 

    Task.Run(() => 
       { 
        Thread.Sleep(2000); 
        Members = new List<string>() {"Alpha", "Beta", "Gamma", "Omega"}; 
       }); 
} 

鱈魚Ë後面的主網頁設置的datacontext(與所有的子控件共享),爲這樣:

public MainWindow() 
{ 
    InitializeComponent(); 

    // Set the windows data context so all controls can have it. 
    DataContext = new MainVM(); 

} 

的網主頁的Xaml結合成員是這樣

<Button Height="30" 
     Width="80" 
     Margin="10" 
     DataContext="{Binding Members}" 
     Content="{Binding Path=[0] }" /> 

<Button Height="30" 
     Width="80" 
     Margin="10" 
     DataContext="{Binding Members}" 
     Content="{Binding Path=[1] }" /> 

<Button Height="30" 
     Width="80" 
     Margin="10" 
     DataContext="{Binding Members}" 
     Content="{Binding Path=[2] }" /> 

<Button Height="30" 
     Width="80" 
     Margin="10" 
     DataContext="{Binding Members}" 
     Content="{Binding Path=[3] }" /> 

結果是這樣的視覺:

enter image description here

我此基礎上我的博客文章中的XAML:ViewModel Main Page Instantiation and Loading Strategy for Easier Binding獲取更多信息和更全面的例子。

0

我認爲你最好的選擇是使用字符串集合並綁定到該集合。您可以在切換切換時更改集合,也可以保留6個集合並綁定到用於切換的集合。

的XAML:

<ItemsControl x:Name="Buttons" ItemsSource="{Binding ButtonTextCollection}"> 
    <ItemsControl.ItemsPanel> 
     <toolkit:WrapPanel/> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Button Width="100" Height="70" Content="{Binding}" Click="OnButtonClick"/> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

代碼隱藏會對您的按鈕事件處理程序單擊

private void OnButtonClick(object sender, RoutedEventArgs e) 
{ 
    var text = ((Button) sender).Content.ToString(); 
    // Send the text 
} 

您的視圖模型將持有ButtonTextCollection財產,將改變基於切換。

public ICollection<string> ButtonTextCollection 
{ 
    get { return _buttonTextCollection; } 
    set 
    { 
     _buttonTextCollection = value; 
     OnPropertyChanged("ButtonTextCollection"); 
    } 
} 

當你要更改的文字,你會改變ButtonTextCollection

public void ChangeButtonText() 
{ 
    ButtonTextCollection = new Collection<string> {"A", "B",...}; 
}