我有一個包含下面的代碼我generic.xaml:如何從實現頁面訪問自定義控件中存在的按鈕?
<ControlTemplate TargetType="local:customVideoControl">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="600"/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<MediaElement x:Name="customMediaPlayer" Source="{TemplateBinding CustomMediaSource}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Height="{TemplateBinding Height}"
Width="{TemplateBinding Width}"
Grid.Row="0" Grid.ColumnSpan="3"
/>
<ToggleButton x:Name="playPauseBtn" Height="50" Width="50" Content="Pause" Grid.Row="1" Grid.Column="0"/>
<Button x:Name="prevBtn" Height="50" Width="50" Content="Prev" Grid.Row="1" Grid.Column="1"/>
<Button x:Name="nextBtn" Height="50" Width="50" Content="Next" Grid.Row="1" Grid.Column="2"/>
</Grid>
</ControlTemplate>
現在就ApplyTemplate在,我訪問控制象下面這樣:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
ToggleButton playPauseBtn = GetTemplateChild("playPauseBtn") as ToggleButton;
Button prevBtn= GetTemplateChild("prevBtn") as Button;
Button nextBtn = GetTemplateChild("nextBtn") as Button;
MediaElement customMediaPlayer = GetTemplateChild("customMediaPlayer") as MediaElement;
playPauseBtn.Checked += (obj, Args) =>
{
customMediaPlayer.Pause();
playPauseBtn.Content = "Play";
};
playPauseBtn.Unchecked += (obj, Args) =>
{
customMediaPlayer.Play();
playPauseBtn.Content = "Pause";
};
nextBtn.Click += (obj, Args) =>
{
customMediaPlayer.Source=new Uri(CustomMediaSource.ToString(),UriKind.RelativeOrAbsolute);
};
prevBtn.Click += (obj, Args) =>
{
customMediaPlayer.Source = new Uri(CustomMediaSource.ToString(), UriKind.RelativeOrAbsolute);
};
}
現在我想acccess的nextBtn,在頁面裏我喜歡落實
CustomVideoControl myVControl=new CustomVideoControl();
這將創建控件的實例,但我想要做的事就點擊next和previous按鈕,thta存在於generic.xaml中的CustomVideoControl內。任何幫助將不勝感激。
感謝, Subhen
它是什麼,你想要做什麼,爲什麼是什麼不是控制功能,似乎沒有一點創建控制,如果你不'將責任移交給控制。 – AnthonyWJones 2010-04-22 11:33:34
我想改變Uri,當我點擊按鈕時,但uri將在主應用程序中可訪問。 有沒有一種方法可以聲明一個依賴的Eventhandler或其他東西,以便可以在單擊主頁面上的該按鈕時調用它。 – Simsons 2010-04-22 11:45:05