如何获取当前绑定到文本框的值?

时间:2013-05-27 18:20:18

标签: c# windows xaml

我需要你的建议,请:)

我在FlipView中有一个绑定到title属性的文本框,它显示了页面的当前标题。

<TextBlock x:Name="jmeno" FontSize="26.667" FontWeight="Light" Text="{Binding Title}" TextWrapping="Wrap" />

我有一个显示视频的媒体元素。

<MediaElement x:Name="mmedia" Source="Video/prvniVideo.mp4" Height="300"  Grid.Row="1" KeyUp="mmedia_KeyUp" />

我想在不同的页面上打开不同的视频,我试图像这样完成它 - 读取texblock的实际显示值,并根据该决定,在media元素中打开哪个视频。

像这样。

        string AktualniStranka;
        AktualniStranka = jmeno.Text;

        if (AktualniStranka == "Exercise 1")
        {
            mmedia.Source = new Uri("ms-appx:/Video/prvniVideo.mp4", UriKind.RelativeOrAbsolute);
            mmedia.Play();
        }
        else
        {
            mmedia.Source = new Uri("ms-appx:/Video/Gumm.mp4", UriKind.RelativeOrAbsolute);
            mmedia.Play();
        }

但它不起作用 - 我试图只阅读文本块的文本并将其放在另一个文本块中,它也不起作用,我花了差不多整整一天试图让它工作..请帮助伙计们:)

完整的XAML代码:

<common:LayoutAwarePage
x:Name="pageRoot"
x:Class="ContosoCookbook.ItemDetailPage"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ContosoCookbook"
xmlns:data="using:ContosoCookbook.Data"
xmlns:common="using:ContosoCookbook.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Page.Resources>

    <!-- Collection of items displayed by this page -->
    <Style x:Name="transportStyle"  TargetType="Button">
        <Setter Property="Height" Value="40" />
        <Setter Property="Width" Value="75" />
        <Setter Property="FontSize" Value="11" />
    </Style>

    <CollectionViewSource
        x:Name="itemsViewSource"
        Source="{Binding Items}"
        d:Source="{Binding AllGroups[0].Items, Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}"/>

    <common:ListConverter x:Key="ListConverter" />

    <Style x:Key="BragAppBarButtonStyle" TargetType="Button" BasedOn="{StaticResource AppBarButtonStyle}">
        <Setter Property="AutomationProperties.AutomationId" Value="BragAppBarButton"/>
        <Setter Property="AutomationProperties.Name" Value="Brag"/>
        <Setter Property="Content" Value="&#xE170;"/>
    </Style>

    <Style x:Key="ReminderAppBarButtonStyle" TargetType="Button" BasedOn="{StaticResource AppBarButtonStyle}">
        <Setter Property="AutomationProperties.AutomationId" Value="ReminderAppBarButton"/>
        <Setter Property="AutomationProperties.Name" Value="Reminder"/>
        <Setter Property="Content" Value="&#xE121;"/>
    </Style>

    <local:ProductLicenseDataSource x:Key="License" />
    <common:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>

</Page.Resources>

<Page.BottomAppBar>
    <AppBar x:Name="PageAppBar" Padding="10,0,10,0">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50*"/>
                <ColumnDefinition Width="50*"/>
            </Grid.ColumnDefinitions>
            <StackPanel x:Name="LeftCommands" Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Left">
                <Button x:Name="BragButton" HorizontalAlignment="Left" Style="{StaticResource BragAppBarButtonStyle}" Click="OnBragButtonClicked" />
                <Button x:Name="PinRecipeButton" HorizontalAlignment="Left" Style="{StaticResource PinAppBarButtonStyle}" Click="OnPinRecipeButtonClicked" />
                <Button x:Name="ReminderButton" HorizontalAlignment="Left" Style="{StaticResource ReminderAppBarButtonStyle}" Click="OnReminderButtonClicked" />
            </StackPanel>
            <StackPanel x:Name="RightCommands" Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Right">
            </StackPanel>
        </Grid>
    </AppBar>
</Page.BottomAppBar>


<!--
    This grid acts as a root panel for the page that defines two rows:
    * Row 0 contains the back button and page title
    * Row 1 contains the rest of the page layout
-->
<Grid
    Style="{StaticResource LayoutRootStyle}"
    DataContext="{Binding Group}"
    d:DataContext="{Binding AllGroups[0], Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}">

    <Grid.RowDefinitions>
        <RowDefinition Height="140"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>


    <!--
        The remainder of the page is one large FlipView that displays details for
        one item at a time, allowing the user to flip through all items in the chosen
        group
    -->


    <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">

                        <!-- Three-column grid for item-detail layout -->
                        <Grid Margin="120,0,20,20">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="600" />
                                <ColumnDefinition Width="40" />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>

                            <StackPanel Orientation="Vertical" Grid.Column="0">
                                <TextBlock x:Name="jmeno" FontSize="26.667" FontWeight="Light" Text="{Binding Title}" TextWrapping="Wrap" />





                            </StackPanel>

                            <StackPanel Orientation="Vertical" Grid.Column="4">
                                <TextBlock FontSize="26.667" FontWeight="Light" Text="Description" Margin="0,0,0,16"/>




                                <TextBlock FontSize="26.667" FontWeight="Light" Text="{Binding ShortDesctiption}" TextWrapping="Wrap"/>
                                <ScrollViewer Style="{StaticResource VerticalScrollViewerStyle}">
                                    <Grid>
                                        <TextBlock FontSize="20" FontWeight="Light" Text="{Binding Directions}" TextWrapping="Wrap" Visibility="{Binding IsLicensed, Source={StaticResource License}, Converter={StaticResource BooleanToVisibilityConverter }}" />
                                        <Button Width="225" Height="120" Background="#30ffffff" Click="OnPurchaseProduct" Visibility="{Binding IsTrial, Source={StaticResource License}, Converter={StaticResource BooleanToVisibilityConverter }}">
                                            <Button.Content>
                                                <TextBlock Text="{Binding FormattedPrice, Source={StaticResource License}}" TextWrapping="Wrap" TextAlignment="Center" />
                                            </Button.Content>
                                        </Button>
                                    </Grid>
                                </ScrollViewer>
                            </StackPanel>
                        </Grid>

                        <VisualStateManager.VisualStateGroups>
                            <!-- Visual states reflect the app's view state inside the FlipView -->
                            <VisualStateGroup x:Name="ApplicationViewStates">
                                <VisualState x:Name="FullScreenLandscape"/>
                                <VisualState x:Name="Filled"/>

                                <!-- Respect the narrower 100-pixel margin convention for portrait -->
                                <VisualState x:Name="FullScreenPortrait">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="image" Storyboard.TargetProperty="MaxHeight">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="400"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>

                                <!-- When snapped, the content is reformatted and scrolls vertically -->
                                <VisualState x:Name="Snapped">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="scrollViewer" Storyboard.TargetProperty="Style">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource VerticalScrollViewerStyle}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="image" Storyboard.TargetProperty="MaxHeight">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="160"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </ScrollViewer>
                </UserControl>
            </DataTemplate>

        </FlipView.ItemTemplate>
    </FlipView>
    <!-- MEDIA PANEL-->

    <StackPanel Orientation="Vertical" Grid.Row="1" >
        <Border   BorderThickness="5" BorderBrush="White" Background="Black"   HorizontalAlignment="Left" Margin="165,50,0,0" VerticalAlignment="Top">
            <ContentControl x:Name="VideoContainer" Height="300" KeyUp="VideoContainer_KeyUp"     >

                <MediaElement x:Name="mmedia" Source="Video/prvniVideo.mp4" Height="300"  Grid.Row="1" KeyUp="mmedia_KeyUp" />
            </ContentControl>
        </Border>

        <StackPanel Orientation="Horizontal" Margin="140,20,0,0">
            <Button Name="btnPlay" Click="btnPlay_Click" 
        Style="{StaticResource transportStyle}" Content="Play" />
            <Button Name="btnPause" Click="btnPause_Click"
        Style="{StaticResource transportStyle}" Content="Pause" />
            <Button Name="btnStop" Click="btnStop_Click"
        Style="{StaticResource transportStyle}" Content="Stop" />
            <Button Name="btnReverse" Click="btnReverse_Click"
        Style="{StaticResource transportStyle}" Content="Rewind" />
            <Button Name="btnForward" Click="btnForward_Click"
        Style="{StaticResource transportStyle}" Content="Forward" />
            <Button Name="btnVolumeUp" Click="btnVolumeUp_Click" 
        Style="{StaticResource transportStyle}" Content="-" />
            <Button Name="btnVolumeDown" Click="btnVolumeDown_Click" 
        Style="{StaticResource transportStyle}" Content="+" />
            <Button Name="btnMute" Click="btnMute_Click" 
        Style="{StaticResource transportStyle}" Content="Mute" />
        </StackPanel>
        <Button x:Name="btnFullScreenToggle" Click="btnFullScreenToggle_Click" 
        Style="{StaticResource transportStyle}" Content="Full Screen ON" Margin="290,0,0,0" Width="302" />

        <TextBlock x:Name="poser" FontSize="26.667" FontWeight="Light" Text="poser" TextWrapping="Wrap" />


    </StackPanel>


    <!-- FlipView used in portrait mode -->
    <FlipView
        x:Name="portraitFlipView"
        AutomationProperties.AutomationId="ItemsFlipView"
        AutomationProperties.Name="Item Details"
        Grid.Row="1"
        Margin="0,-3,20,0"
        ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
        Visibility="Collapsed">

        <FlipView.ItemTemplate>
            <DataTemplate>
                <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
                    <ScrollViewer x:Name="scrollViewer" Style="{StaticResource VerticalScrollViewerStyle}" Grid.Row="1">

                        <!-- Vertical StackPanel for item-detail layout -->
                        <StackPanel Orientation="Vertical" Margin="100,0,20,0">
                            <StackPanel Orientation="Vertical">
                                <TextBlock FontSize="26.667" FontWeight="Light" Text="{Binding Title}" TextWrapping="Wrap"/>
                                <Image x:Name="image" Width="400" Margin="0,20,0,40" Stretch="Uniform" Source="{Binding Image}" HorizontalAlignment="Left"/>
                            </StackPanel>

                            <StackPanel Orientation="Vertical">
                                <TextBlock FontSize="26.667" FontWeight="Light" Text="Ingredients" Margin="0,0,0,16"/>
                                <TextBlock FontSize="20" FontWeight="Light" LineHeight="32.5" Text="{Binding Ingredients, Converter={StaticResource ListConverter}}" TextWrapping="Wrap" />
                            </StackPanel>

                            <StackPanel Orientation="Vertical">
                                <TextBlock FontSize="26.667" FontWeight="Light" Text="Directions" Margin="0,24,0,16"/>
                                <ScrollViewer Style="{StaticResource VerticalScrollViewerStyle}">
                                    <Grid>
                                        <TextBlock FontSize="20" FontWeight="Light" Text="{Binding Directions}" TextWrapping="Wrap" Visibility="{Binding IsLicensed, Source={StaticResource License}, Converter={StaticResource BooleanToVisibilityConverter }}" />
                                        <Button Width="225" Height="120" Background="#30ffffff" Click="OnPurchaseProduct" Visibility="{Binding IsTrial, Source={StaticResource License}, Converter={StaticResource BooleanToVisibilityConverter }}">
                                            <Button.Content>
                                                <TextBlock Text="{Binding FormattedPrice, Source={StaticResource License}}" TextWrapping="Wrap" TextAlignment="Center" />
                                            </Button.Content>
                                        </Button>

                                    </Grid>
                                </ScrollViewer>
                            </StackPanel>
                        </StackPanel>
                    </ScrollViewer>
                </UserControl>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>

    <FlipView
        x:Name="snappedFlipView"
        AutomationProperties.AutomationId="ItemsFlipView"
        AutomationProperties.Name="Item Details"
        Grid.Row="1"
        Margin="0,-3,0,0"
        ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
        Visibility="Collapsed">

        <FlipView.ItemTemplate>
            <DataTemplate>
                <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
                    <ScrollViewer x:Name="scrollViewer" Style="{StaticResource VerticalScrollViewerStyle}" Grid.Row="1">

                        <!-- Vertical StackPanel for item-detail layout -->
                        <StackPanel Orientation="Vertical" Margin="20,0,20,0">
                            <StackPanel Orientation="Vertical">
                                <TextBlock FontSize="20" FontWeight="Light" Text="{Binding Title}" TextWrapping="Wrap"/>
                                <Image x:Name="image" Width="260" Margin="0,12,0,40" Stretch="Uniform" Source="{Binding Image}" HorizontalAlignment="Left"/>
                            </StackPanel>
                            <StackPanel Orientation="Vertical">
                                <TextBlock FontSize="20" FontWeight="Light" Text="Ingredients" Margin="0,0,0,16"/>
                                <TextBlock FontSize="16" FontWeight="Light" TextWrapping="Wrap" Text="{Binding Ingredients, Converter={StaticResource ListConverter}}" />
                            </StackPanel>
                        </StackPanel>
                    </ScrollViewer>
                </UserControl>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>

    <!-- Back button and page title -->
    <Grid HorizontalAlignment="Left" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
        <TextBlock x:Name="pageTitle" Text="{Binding Title}" Style="{StaticResource PageHeaderTextStyle}" Grid.Column="1" IsHitTestVisible="false"/>
        <Button Name="btnFullScreenOff" Click="btnFullScreenToggle_Click"   
        Style="{StaticResource transportStyle}" Content="Full Screen OFF" HorizontalAlignment="Right" Grid.Column="1" Margin="0,56,495,36" RenderTransformOrigin="-9.161,0.266" Height="48" Width="106" Visibility="Collapsed" />
    </Grid>

    <VisualStateManager.VisualStateGroups>

        <!-- Visual states reflect the application's view state -->
        <VisualStateGroup x:Name="ApplicationViewStates">
            <VisualState x:Name="FullScreenLandscape"/>
            <VisualState x:Name="Filled"/>

            <!-- The back button respects the narrower 100-pixel margin convention for portrait -->
            <VisualState x:Name="FullScreenPortrait">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                    </ObjectAnimationUsingKeyFrames>

                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="flipView" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="portraitFlipView" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                    </ObjectAnimationUsingKeyFrames>

                </Storyboard>
            </VisualState>

            <!-- The back button and title have different styles when snapped -->
            <VisualState x:Name="Snapped">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="flipView" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="snappedFlipView" Storyboard.TargetProperty="Visibility">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                    </ObjectAnimationUsingKeyFrames>

                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</Grid>

完整的C#代码:

1 个答案:

答案 0 :(得分:0)

我看到你试图从你的Textblock(jmeno)的名字中获取显示的值,对吗?

你不应该这样做。您的文本块在datatemplate中定义,为其指定名称的唯一原因是应用故事板。

你应该做的是获取Flipview的SelectedItem对象(这是你的可视元素datacontext),并获取该数据对象的Title属性。

所以:

(flipView.SelectedItem as -WhateverYourTypeIs - )。标题,你应该有你的价值。