我有一个页面,其中包含某些控件,如文本框和按钮。我正在尝试访问这些控件的属性以在其中显示文本。
我可以访问他们的属性,但是我实际上遇到了麻烦,实际上是让这些值发挥作用。
到目前为止,我有这个:
Pages.pgEditPlayer editPlayerPage = new Pages.pgEditPlayer();
editPlayerPage.txtPlayerName.Text = "Hello, World";
这使我可以访问文本框而不会出现任何错误,但是当我检查文本框是否已更新时,它不会显示“Hello,World”。有谁知道为什么会这样?
XAML:
<Window x:Class="ZimbuyeManagementApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Zimbuye Management Application" Height="358.955" Width="738.433">
<Window.Resources>
<XmlDataProvider x:Key="views">
<x:XData>
<Views xmlns="">
<View>
<Page Source="Pages\pgAddPlayer.xaml"/>
</View>
<View>
<Page Source="Pages\pgRemovePlayer.xaml"/>
</View>
<View>
<Page Source="Pages\pgEditPlayer.xaml"/>
</View>
</Views>
</x:XData>
</XmlDataProvider>
<CollectionViewSource x:Key="ViewSource" Source="{Binding Source={StaticResource views}, XPath=Views/View}"/>
<Storyboard x:Key="slideLeftToRight" TargetProperty="RenderTransform.(TranslateTransform.X)" AccelerationRatio=".4" DecelerationRatio=".4">
<DoubleAnimation Storyboard.TargetName="viewer" Duration="0:0:0.6" From="512" To="0" />
<DoubleAnimation Storyboard.TargetName="bordervisual" Duration="0:0:0.6" From="0" To="-512" />
</Storyboard>
<Storyboard x:Key="slideRightToLeft" TargetProperty="RenderTransform.(TranslateTransform.X)" AccelerationRatio=".4" DecelerationRatio=".4">
<DoubleAnimation Storyboard.TargetName="viewer" Duration="0:0:0.6" From="-512" To="0" />
<DoubleAnimation Storyboard.TargetName="bordervisual" Duration="0:0:0.6" From="0" To="512" />
</Storyboard>
<VisualBrush x:Key="VisualBrush1" Visual="{Binding ElementName=viewer}" />
</Window.Resources>
<Grid x:Name="frmMain" Loaded="frmMain_Loaded">
<TabControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
<TabItem Width="128" FontSize="18" Height="128">
<TabItem.Background>
<ImageBrush ImageSource="C:\Users\Sythnet-PC\documents\visual studio 2012\Projects\ZimbuyeManagementApplication\ZimbuyeManagementApplication\Images\home.png"></ImageBrush>
</TabItem.Background>
<StackPanel Margin="10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock Text="Zimbuye Management Application" FontFamily="comic sans ms" FontSize="24"/>
<TextBlock x:Name="txtWelcomeMessage" Text="Welcome!" FontFamily="comic sans ms" FontSize="16"/>
<StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Center" VerticalAlignment="Bottom">
<Border BorderThickness="1" BorderBrush="Black">
<TextBlock x:Name="txtAuthor" Margin="5"> Author </TextBlock>
</Border>
<Border BorderThickness="1" BorderBrush="Black">
<TextBlock x:Name="txtAppName" Margin="5"> Application Name </TextBlock>
</Border>
<Border BorderThickness="1" BorderBrush="Black">
<TextBlock x:Name="txtVersionNumber" Margin="5"> Version Number </TextBlock>
</Border>
<Border BorderThickness="1" BorderBrush="Black">
<TextBlock x:Name="txtStartDate" Margin="5"> Start Date </TextBlock>
</Border>
</StackPanel>
</StackPanel>
</TabItem>
<TabItem Header="Create Player" Width="128" FontSize="18" Height="128">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="26"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox Grid.Row="0" Grid.Column="0" x:Name="lstPlayers" Width="250" SelectionChanged="lstPlayers_SelectionChanged" HorizontalAlignment="Left"/>
<TextBox Grid.Row="1" Grid.Column="0" x:Name="txtSearch" Width="250" TextChanged="txtSearch_TextChanged" HorizontalAlignment="Left"/>
<StackPanel Grid.Column="1">
<Grid Name="ViewGrid" Height="512" Width="512">
<Border x:Name="bordervisual" Width="512">
<Rectangle x:Name="rectanglevisual" />
<Border.RenderTransform>
<TranslateTransform />
</Border.RenderTransform>
</Border>
<ItemsControl x:Name="viewer" DataContext="{StaticResource ViewSource}" ItemsSource="{Binding XPath=Page}" Margin="0,0,0,1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Frame x:Name="frame" Source="{Binding XPath=@Source}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.RenderTransform>
<TranslateTransform />
</ItemsControl.RenderTransform>
</ItemsControl>
<StackPanel Orientation="Horizontal">
<Ellipse Width="32" Height="32" MouseDown="Ellipse_MouseDown_1" Margin="10">
<Ellipse.Fill>
<SolidColorBrush Color="#FF6E8AE8" />
</Ellipse.Fill>
</Ellipse>
<Ellipse Width="32" Height="32" MouseDown="Ellipse_MouseDown_2" Margin="10">
<Ellipse.Fill>
<SolidColorBrush Color="#FF6E8AE8" />
</Ellipse.Fill>
</Ellipse>
<Ellipse Width="32" Height="32" MouseDown="Ellipse_MouseDown_3" Margin="10">
<Ellipse.Fill>
<SolidColorBrush Color="#FF6E8AE8" />
</Ellipse.Fill>
</Ellipse>
</StackPanel>
</Grid>
</StackPanel>
<StackPanel Grid.Column="2">
<TextBlock Margin="5" Grid.Row="0" Grid.Column="1" x:Name="lblPlayerName" Height="26" Width="300" HorizontalAlignment="Left" Text="Name: "/>
<TextBlock Margin="5" Grid.Row="0" Grid.Column="1" x:Name="lblPlayerSurname" Height="26" Width="300" HorizontalAlignment="Left" Text="Surname:"/>
<TextBlock Margin="5" Grid.Row="0" Grid.Column="1" x:Name="lblPlayerAge" Height="26" Width="300" HorizontalAlignment="Left" Text="Age: "/>
<TextBlock Margin="5" Grid.Row="0" Grid.Column="1" x:Name="lblPlayerCode" Height="26" Width="300" HorizontalAlignment="Left" Text="Code: "/>
<TextBlock Margin="5" Grid.Row="0" Grid.Column="1" x:Name="lblPlayerStatus" Height="26" Width="300" HorizontalAlignment="Left" Text="Status: "/>
</StackPanel>
</Grid>
</TabItem>
<TabItem Header="Create Team" Width="128" FontSize="18" Height="128">
</TabItem>
<TabItem Header="Assign Players" Width="128" FontSize="18" Height="128">
</TabItem>
<TabItem Header="Admin" Width="128" FontSize="18" Height="128" Visibility="Hidden">
</TabItem>
</TabControl>
</Grid>
</Window>
答案 0 :(得分:0)
也许尝试调用属于MainWindow的实例:
在主窗口的代码隐藏中:
this.MyPage.txtPlayerName.Text = "Hello, World";
答案 1 :(得分:0)
从框架访问当前页面:
((Pages.pgEditPlayer)frame.Content).txtPlayerName.Text = "Hello, World";
修改强>
在这种情况下,我们需要从DataTemplate中提取Frame元素。 (只是为了挑战,因为它似乎有点疯狂)
var Pages = new Page[viewer.Items.Count];
for (int i = 0; i < Pages.Length ; i++)
{
var contentPrs = (ContentPresenter)viewer.ItemContainerGenerator.ContainerFromIndex(i);
contentPrs.ApplyTemplate();
var template = contentPrs.ContentTemplate;
var currFrame = (Frame)template.FindName("frame", contentPrs );
Pages[i] = (Page)currFrame.Content;
}
((Pages.pgEditPlayer)Pages.First(p => p is pgEditPlayer)).txtPlayerName.Text = "";
希望有效......