如何在WPF中的另一个页面文本框中访问要绑定的页面元素?

时间:2016-12-29 07:39:35

标签: c# wpf xaml

我有一个简单的问题。我尝试了太多而且没有解决。我有一个WPF窗口和一个页面。单击该按钮时,页面将显示在主窗口的框架上。它用作主窗口上的DataTrigger按钮,并希望主窗口中的按钮触发,而另一个页面的textbox被聚焦。

Main.xaml:

<Window x:Class="example.Main"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        Title="Main Menu" >
    <Window.Resources>
        <Style x:Key="CustomStyleButton" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="#373737" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontSize" Value="15" />
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border CornerRadius="4" Background="{TemplateBinding Background}">
                            <Grid>
                                <Path x:Name="PathIcon" Width="15" Height="25" Stretch="Fill" Fill="#4C87B3" HorizontalAlignment="Left" Margin="17,0,0,0"  Data="F1 M 30.0833,22.1667L 50.6665,37.6043L 50.6665,38.7918L 30.0833,53.8333L 30.0833,22.1667 Z "/>
                                <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,10"  />
                            </Grid>
                        </Border>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="#E59400" />
                                <Setter Property="Foreground" Value="White" />
                                <Setter TargetName="PathIcon" Property="Fill" Value="Black" />
                            </Trigger>

                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Background" Value="OrangeRed" />
                                <Setter Property="Foreground" Value="White" />
                            </Trigger>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="Background" Value="Gold" />
                                <Setter Property="Foreground" Value="White" />
                            </Trigger>


       // This needs to be triggered as long as focussing another page's textboxt.
                            <DataTrigger Binding="{Binding ElementName=Exam , Path=txtSearch.IsFocused}"
                     Value="true">
                                        <Setter Property="Background" Value="Red" />
                                    </DataTrigger>
                                                                                 </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

        </Style>      

    </Window.Resources>

        <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="207*"/>
            <ColumnDefinition Width="722*"/>
        </Grid.ColumnDefinitions>
        <StackPanel  Margin="20,0,0,0" >

            <Button x:Name="button"  x:FieldModifier="public" Style="{StaticResource CustomStyleButton}" Content="user" Click="button_Click"/>
            <Label x:Name="label_Copy2" Margin="0,0,0.2,0" Height="21"/>
            <Button x:Name="button1" Style="{StaticResource CustomStyleButton}" Content="EXAMPLE" Click="button1_Click"/>
            <Label x:Name="label_Copy1" Margin="0,0,0.2,0" Height="21"/>

        </StackPanel>


        <DockPanel Grid.Column="1" Margin="44,80,44,65" Background="#FFA3D340" >
            <Frame x:Name="_mainFrame" BorderBrush="#FFBC9D0D" BorderThickness="5"  NavigationUIVisibility="Hidden"  />

        </DockPanel>

    </Grid>

</Window>



enter code here

的Page1.xaml:

<Page x:Class="example.Page1_1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:example"
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
      Title="Page1_1" x:Name="Exam" >
    <Page.Style>
        <Style TargetType="Page">
            <Setter Property="Background" Value="#CCCCD0" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=txtSearch, Path=IsFocused}"
                     Value="true">
                    <Setter Property="Background" Value="Black" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Page.Style>

    <TextBox x:Name="txtSearch" x:FieldModifier="public" Width="100"
           HorizontalAlignment="Center" VerticalAlignment="Center" />
</Page>

1 个答案:

答案 0 :(得分:0)

将一个公共属性添加到Page1.xaml.cs类,该类返回对TextBox的引用:

public partial class Page1 : Page
{
    public Page1()
    {
        InitializeComponent();
    }

    public TextBox TxtSearch
    {
        get { return txtSearch; }
    }
}

...并在窗口中的Style中绑定它:

<Style x:Key="CustomStyleButton" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#373737" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontSize" Value="15" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border CornerRadius="4" Background="{TemplateBinding Background}">
                    <Grid>
                        <Path x:Name="PathIcon" Width="15" Height="25" Stretch="Fill" Fill="#4C87B3" HorizontalAlignment="Left" Margin="17,0,0,0"  Data="F1 M 30.0833,22.1667L 50.6665,37.6043L 50.6665,38.7918L 30.0833,53.8333L 30.0833,22.1667 Z "/>
                        <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,10"  />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#E59400" />
                        <Setter Property="Foreground" Value="White" />
                        <Setter TargetName="PathIcon" Property="Fill" Value="Black" />
                    </Trigger>

                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" Value="OrangeRed" />
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="Background" Value="Gold" />
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Content.TxtSearch.IsFocused, ElementName=_mainFrame}" Value="True">
            <Setter Property="Background" Value="Red" />
        </DataTrigger>
    </Style.Triggers>
</Style>

只要您使用以下属性将框架的内容设置为页面,这将起作用:

<Frame x:Name="_mainFrame" BorderBrush="#FFBC9D0D" BorderThickness="5"  NavigationUIVisibility="Hidden"
       Source="Page1.xaml"/>