2011-04-20 18 views
0

我有一個用戶控件將故事板動畫應用於控件。當在頁面中點擊一個按鈕時,故事板就會啓動並基本上可視化地向用戶呈現控件。故事板駐留在當前頁面作爲資源在我的當前項目文件中使用Silverlight用戶控件中的路由事件

<navigation:Page.Resources> 
    <Storyboard x:Name="PreferncesOpen">....</Storyboard x:Name="PreferncesOpen"> 
     </navigation:Page.Resources> 

內頁我有按鈕,我有一個click事件上啓動該故事板

private void btnOpenPreferences_Click(object sender, RoutedEventArgs e) 
    { 
     preferencesPanel.Visibility = System.Windows.Visibility.Visible; 
     PreferncesOpen.Begin(); 
    } 

在用戶控件(preferencesPanel)我有一個點擊時按鈕需要關閉/摺疊用戶控件。我打算使用Visibility.collapsed來做到這一點。我假設我需要使用路由命令,因爲按鈕位於用戶控件中,但是操作需要在包含控件的頁面中調用?我對路由命令仍然陌生,我認爲這是正確的方法。我只是不確定如何點擊用戶控件中的按鈕,並修改或執行可能影響頁面(控件所在的頁面)可能會發生變化的命令,或者該頁面會影響頁面中的其他元素?例如,當在用戶控件中單擊按鈕時,我希望將用戶控件的可見性設置爲摺疊狀態。我也希望在主頁面中的一個網格列的寬度重新調整大小。我在過去使用了頁面背後的代碼,但我試圖分離這些內容,並且我認爲路由命令將成爲一種方式。 我非常感謝任何提示。

預先感謝您

回答

0

標題是有點誤導,你問的命令而不是路由事件,如果我理解正確。

下面是使用Prism庫中的DelegateCommand<T>的示例;這恰好是我個人的偏好。

標記:

<Button x:Name="MyButton" Content="Btn" Command="{Binding DoSomethingCommand}"/> 

代碼隱藏*或視圖模型:

(*如果你不使用MVVM確保添加MyButton.DataContext = this;所以你確認按鈕可以有效地將數據綁定到您的代碼中)

public DelegateCommand<object> DoSomethingCommand 
{ 
    get 
    { 
     if(mDoSomethingCommand == null) 
      mDoSomethingCommand = new DelegateCommand(DoSomething, canDoSomething); 
     return mDoSomethingCommand; 
    } 

private DelegateCommand<object> mDoSomethingCommand; 

// here's where the command is actually executed 
void DoSomething(object o) 
{} 

// here's where the check is made whether the command can actually be executed 
// insert your own condition here 
bool canDoSomething(object o) 
{ return true; } 


// here's how you can force the command to check whether it can be executed 
// typically a reaction for a PropertyChanged event or whatever you like 
DoSomethingCommand.RaiseCanExecuteChanged(); 

參數是pa ssed爲上述函數是CommandParameter依賴項屬性(在Prism中,它是一個附屬屬性以及Command屬性,如果內存爲我服務的話)。 設置後,您可以將您選擇的值傳遞給您希望執行的命令。

希望有所幫助。

相關問題