从页面更新窗口上的文本块

时间:2013-01-16 23:35:09

标签: c# wpf

我使用WPF制作了一个小应用程序,它有一些控件。其中一个控件是名为Credit的Textblock,另一个控件是Frame,在该框架内页面是打开的。例如:SendMessage.xaml

我希望有人帮助我,解释是否有办法按下SendMessage.xaml页面内的按钮,并在包含框架的窗口中更新Textblock。

我的窗口xaml

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:testeSMS"
xmlns:Properties="clr-namespace:testeSMS.Properties" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" mc:Ignorable="d"
x:Class="testeSMS.JanelaPrincipal"
x:Name="Window"
Title="JanelaPrincipal"
Width="640" Height="480" WindowStyle="None" BorderThickness="1" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
<Window.Resources>



    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0"/>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="closeBT">
                                            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <local:CloseBT x:Name="closeBT" Foreground="{x:Null}" Opacity="0.3"/>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="" OpacityMask="Black"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ButtonStyle3" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0.5"/>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="label">
                                            <EasingColorKeyFrame KeyTime="0" Value="#FF33FFFF"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Label x:Name="label" Content="Nome da APP by  © Kineth Networks" Foreground="#FF3399FF" FontSize="8"/>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content=""/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <LinearGradientBrush x:Key="MenuBackground" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#F6F6F6" Offset="0.25"/>
        <GradientStop Color="#EAE8E8" Offset="0.25"/>
        <GradientStop Color="#DCD9D9" Offset="0.8"/>
        <GradientStop Color="White" Offset="1"/>
    </LinearGradientBrush>
    <Style x:Key="MenuStyle1" TargetType="{x:Type Menu}">
        <Setter Property="Background" Value="{StaticResource MenuBackground}"/>
        <Setter Property="FontFamily" Value="{DynamicResource {x:Static SystemFonts.MenuFontFamilyKey}}"/>
        <Setter Property="FontSize" Value="{DynamicResource {x:Static SystemFonts.MenuFontSizeKey}}"/>
        <Setter Property="FontStyle" Value="{DynamicResource {x:Static SystemFonts.MenuFontStyleKey}}"/>
        <Setter Property="FontWeight" Value="{DynamicResource {x:Static SystemFonts.MenuFontWeightKey}}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Menu}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMainMenu" Value="True"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="RadPanelBarStyle1" TargetType="{x:Type telerik:RadPanelBar}">
        <Setter Property="KeyboardNavigation.TabNavigation" Value="Local"/>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <telerik:PanelBarPanel IsItemsHost="True"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type telerik:RadPanelBar}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="OrientationStates">
                                <VisualState x:Name="Vertical">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="(telerik:LayoutTransformControl.LayoutTransform)" Storyboard.TargetName="transformationRoot">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <RotateTransform Angle="0"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Horizontal">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="(telerik:LayoutTransformControl.LayoutTransform)" Storyboard.TargetName="transformationRoot">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <RotateTransform Angle="-90"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <telerik:LayoutTransformControl x:Name="transformationRoot" IsTabStop="False" Foreground="#FF2D2C2C">
                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                                <ScrollViewer x:Name="ScrollViewer" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" HorizontalScrollBarVisibility="Auto" IsTabStop="False" Margin="0,-1,0,0" Padding="{TemplateBinding Padding}" VerticalScrollBarVisibility="Auto" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
                                    <telerik:StyleManager.Theme>
                                        <telerik:Expression_DarkTheme/>
                                    </telerik:StyleManager.Theme>
                                    <ItemsPresenter/>
                                </ScrollViewer>
                            </Border>
                        </telerik:LayoutTransformControl>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Orientation" Value="Vertical"/>
        <Setter Property="IsTabStop" Value="True"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="Foreground" Value="#FFDDDDDD"/>
        <Setter Property="BorderBrush" Value="#FF595959"/>
        <Setter Property="Background" Value="#FF3D3D3D"/>
        <Setter Property="BorderThickness" Value="1"/>
    </Style>
    <Style x:Key="ButtonStyle2" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ButtonFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#F3F3F3" Offset="0"/>
        <GradientStop Color="#EBEBEB" Offset="0.5"/>
        <GradientStop Color="#DDDDDD" Offset="0.5"/>
        <GradientStop Color="#CDCDCD" Offset="1"/>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
    <Style x:Key="ButtonStyle4" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" OpacityMask="Black"/>
                    </Microsoft_Windows_Themes:ButtonChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="true">
                            <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ButtonStyle5" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="label">
                                            <EasingColorKeyFrame KeyTime="0" Value="#FF4FAAFF"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Label x:Name="label" Content="[Logout]" Foreground="White" FontSize="10.667"/>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content=""/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Window.Background>
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="#00F0F0FF" Offset="0"/>
        <GradientStop Color="White" Offset="1"/>
    </LinearGradientBrush>
</Window.Background>
<Window.Effect>
    <DropShadowEffect/>
</Window.Effect>

<Grid x:Name="LayoutRoot">
    <Grid.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FFF0F0FF" Offset="0"/>
            <GradientStop Color="White" Offset="1"/>
        </LinearGradientBrush>
    </Grid.Background>
    <Rectangle MouseDown="Rectangle_MouseDown_1" Height="66" VerticalAlignment="Top" Width="639">
        <Rectangle.Stroke>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF1D2D42" Offset="0"/>
                <GradientStop Color="#FF35537A" Offset="1"/>
            </LinearGradientBrush>
        </Rectangle.Stroke>
        <Rectangle.OpacityMask>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="0"/>
                <GradientStop Color="Black" Offset="1"/>
            </LinearGradientBrush>
        </Rectangle.OpacityMask>
        <Rectangle.Fill>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF1D2D42" Offset="0"/>
                <GradientStop Color="#FF35537A" Offset="1"/>
            </LinearGradientBrush>
        </Rectangle.Fill>
    </Rectangle>
    <Image Height="66" Margin="0,0,477.5,0" Source="logo2.png" Stretch="Fill" VerticalAlignment="Top" OpacityMask="Black">
        <Image.Effect>
            <DropShadowEffect BlurRadius="10" RenderingBias="Quality" ShadowDepth="4"/>
        </Image.Effect>
    </Image>

    <Button Content="Button" Click="Button_Click_1" Height="10" Margin="0,0,6,464" Style="{DynamicResource ButtonStyle1}" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="12"/>
    <Button Content="Button" Click="Button_Click_2" Height="1.667" Margin="0,13.583,20.653,0" Style="{DynamicResource ButtonStyle1}" VerticalAlignment="Top" HorizontalAlignment="Right" Width="9.833"/>

    <Label Content="Nome da App © Kineth Networks" Margin="285.5,0,160.006,-2.96" VerticalAlignment="Bottom" FontSize="9.333" Foreground="#80000000" FontWeight="Bold"/>
    <Frame x:Name="mainFrame" Content=". . ." Margin="0,66,0,23.453" HorizontalAlignment="Right" Width="513" NavigationUIVisibility="Hidden" />
    <DockPanel LastChildFill="False" Margin="0,66,0,0" HorizontalAlignment="Left" Width="125" Background="#FF3D3D3D">
        <telerik:RadPanelBar  Focusable="False" Height="174.5" Width="123" Margin="0,0,0,235.5" telerik:StyleManager.Theme="Expression_Dark" BorderThickness="0" Background="#FF3D3D3D" BorderBrush="Black" VerticalAlignment="Bottom" HorizontalAlignment="Center" Style="{DynamicResource RadPanelBarStyle1}">
            <telerik:RadPanelBarItem DropPosition="Inside" Header="Contactos" Width="123" Background="{x:Null}" BorderBrush="#5A000000" FontWeight="Bold" Margin="0,0,0,1" BorderThickness="1,0,1,1">
                <telerik:RadPanelBarItem Click="RadPanelBarItem_Click_2" Header="Inserir" Width="100" HorizontalAlignment="Left" Margin="0,2,0,0"/>
                <telerik:RadPanelBarItem Click="RadPanelBarItem_Click_5" Header="Listar" Width="100" HorizontalAlignment="Left" Margin="0,1,0,0"/>
            </telerik:RadPanelBarItem>
            <telerik:RadPanelBarItem DropPosition="Inside" Header="Grupos" Width="123" Background="{x:Null}" BorderBrush="#59000000" FontWeight="Bold" Margin="0,0,0,1">
                <telerik:RadPanelBarItem Click="RadPanelBarItem_Click_3" Header="Inserir" Width="100" HorizontalAlignment="Left" Margin="0,2,0,0"/>
                <telerik:RadPanelBarItem Click="RadPanelBarItem_Click_4" Header="Listar" Width="100" HorizontalAlignment="Left" Margin="0,1,0,0" IsExpanded="True"/>
            </telerik:RadPanelBarItem>
            <telerik:RadPanelBarItem DropPosition="Inside" Header="SMS" Width="123" Background="{x:Null}" BorderBrush="#59000000" FontWeight="Bold" Margin="0,0,0,1">
                <telerik:RadPanelBarItem Click="RadPanelBarItem_Click_1" Header="Enviar" Width="100" HorizontalAlignment="Left" Margin="0,2,0,0"/>
            </telerik:RadPanelBarItem>
            <telerik:RadPanelBarItem DropPosition="Inside" Header="Info" Width="123" Background="{x:Null}" BorderBrush="#59000000" FontWeight="Bold" Margin="0,0,0,1">
                <telerik:RadPanelBarItem Header="Historico" Width="100" HorizontalAlignment="Left" Margin="0,1,0,0"/>
                <telerik:RadPanelBarItem Header="Creditos" Width="100" HorizontalAlignment="Left" Margin="0,1,0,0"/>
            </telerik:RadPanelBarItem>
            <telerik:RadPanelBarItem DropPosition="Inside" Header="Pacotes" Width="123" Background="{x:Null}" BorderBrush="#59000000" FontWeight="Bold" Margin="0,0,0,1" BorderThickness="1,1,1,2">
                <telerik:RadPanelBarItem Header="Gerir" Width="100" HorizontalAlignment="Left" Margin="0,1,0,0"/>
            </telerik:RadPanelBarItem>
        </telerik:RadPanelBar>
    </DockPanel>
    <TextBlock  x:Name="labelCredito" Text="0 sms" HorizontalAlignment="Right" Margin="0,51,68,0" VerticalAlignment="Top" Foreground="White" FontFamily="/testeSMS;component/Fonts/#Segoe UI" FontSize="10.667" IsManipulationEnabled="True"/>
    <Label Content="Username:" HorizontalAlignment="Right" Margin="0,30,61.899,0" VerticalAlignment="Top" FontSize="10.667" Foreground="White"/>
    <Label x:Name="lb_username" Content="Son Goku" HorizontalAlignment="Right" Margin="0,30,0,0" VerticalAlignment="Top" Foreground="White" FontSize="10.667" FontWeight="Bold"/>
    <Button Content="Button" HorizontalAlignment="Right" Height="23.959" Margin="0,45,5.5,0" Style="{DynamicResource ButtonStyle5}" VerticalAlignment="Top" Width="49.879" Click="Logout_Butt"/>

</Grid>

打开页面的代码隐藏文件中的方法

private void RadPanelBarItem_Click_1(object sender, Telerik.Windows.RadRoutedEventArgs e)
{

    mainFrame.Source = new Uri("SmS_Enviar.xaml", UriKind.Relative);
}

并且在SmS_Enviar.xaml页面上我有一个方法,当我按下按钮执行某些操作时,操作完成后我想更新我的“JanelaPrincipal”窗口上的标签

1 个答案:

答案 0 :(得分:0)

您可以使用MVVMLight's Messenger或mediator pattern的其他实现。让页面中的代码发送消息,并且包含的​​窗口代码订阅消息。