8

Flipview の datatemplate 内にある MediaElement を持っています。コード ビハインドで「video」という名前の MediaElement にアクセスして、ボタンを介して再生、一時停止などのプロパティを割り当てることができるようにします。やろうとしている:

    <FlipView
    x:Name="flipView"
    AutomationProperties.AutomationId="ItemsFlipView"
    AutomationProperties.Name="Item Details"
    TabIndex="1"
    Grid.RowSpan="2"
    ItemsSource="{Binding Source={StaticResource itemsViewSource}}">

    <FlipView.ItemContainerStyle>
        <Style TargetType="FlipViewItem">
            <Setter Property="Margin" Value="0,137,0,0"/>
        </Style>
    </FlipView.ItemContainerStyle>

    <FlipView.ItemTemplate>
        <DataTemplate>
            <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
                <ScrollViewer x:Name="scrollViewer" Style="{StaticResource VerticalScrollViewerStyle}" Grid.Row="1">
                    <Grid Margin="120,0,20,20">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="400" />
                            <ColumnDefinition Width="40" />
                            <ColumnDefinition Width="360" />
                            <ColumnDefinition Width="40" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Border BorderBrush="Black" BorderThickness="1" Width="350" HorizontalAlignment="Left" Grid.Row="0">
                            <MediaElement x:Name="Video" AutomationProperties.Name="Video" Source="/Assets/Big_Buck_Bunny.mp4" HorizontalAlignment="Center" VerticalAlignment="Stretch" Height="250" Width="350" AutoPlay="True" IsLooping="True" />
                        </Border>
                        <Border BorderBrush="Black" BorderThickness="1" Height="65" Width="350" HorizontalAlignment="Left" Grid.Row="1">
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <Button x:Name="playButton" Margin="0,0" Click="playButton_Click" Style="{StaticResource PlayAppBarButtonStyle}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                <Button x:Name="pauseButton" Margin="0,0" Click="pauseButton_Click" Style="{StaticResource PauseAppBarButtonStyle}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </StackPanel>
                        </Border>
                    </Grid>
                </ScrollViewer>
            </UserControl>
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipView>

どうすれば意図したものを達成できますか?

4

3 に答える 3

20

次のことを試してください。

    private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
    {
        int childNumber = VisualTreeHelper.GetChildrenCount(control);
        for (int i = 0; i < childNumber; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(control, i);
            FrameworkElement fe = child as FrameworkElement;
            // Not a framework element or is null
            if (fe == null) return null;

            if (child is T && fe.Name == ctrlName)
            {
                // Found the control so return
                return child;
            }
            else
            {
                // Not found it - search children
                DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
                if (nextLevel != null)
                    return nextLevel;
            }
        }
        return null;
    }

次に、再生/一時停止ボタン ボタンから呼び出します。

    MediaElement media = FindChildControl<MediaElement>(this, "media") as MediaElement;
    media.Play();

この件に関する関連ブログ投稿

于 2013-03-03T21:57:27.410 に答える
1

私は約 1 年前にこのトピックに関するブログ記事を書きました。おそらくそれはあなたを助けるでしょう:http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html

あらすじはこうです。ビジュアル ツリーを解析してすべての要素を取得する必要があります。その後、LINQ などを使用して結果をフィルター処理し、オブジェクトを取得できます。

于 2013-03-03T22:48:34.920 に答える
0

Jerry のソリューションを拡張して、必要なコントロールのみを取得し、再帰呼び出し中に中間リストを作成しない、より柔軟でパフォーマンスの高いソリューションにしました。

そのように単純に使用して、コントロールを取得できます。

var myControl = AllChildren(parent, c => c.Name == "xxx").FirstOrDefault();

そのためには、次の AllChildren 関数を含める必要があります。

 private List<Control> AllChildren(DependencyObject parent, Func<DependencyObject, bool> query,   List<Control> _List = null ) 
    {
        if (_List == null)
             _List = new List<Control>();

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var _Child = VisualTreeHelper.GetChild(parent, i);
            if (_Child is Control && query(_Child))
            {

                _List.Add(_Child as Control);
            }
            AllChildren(_Child, query, _List);
        }
        return _List;
    }
于 2017-01-01T15:46:43.773 に答える