Как получить доступ к элементу страницы для привязки в текстовом поле другой страницы в WPF?

У меня простой вопрос. Я слишком много пытался и не решил. У меня есть одно окно WPF и одна страница. При нажатии на кнопку страница отображается на рамке главного окна. Он используется в качестве кнопки DataTriggerfor На главном окне и хотел бы, чтобы кнопка в главном окне, чтобы вызвать в то время как другая страница textboxсосредоточена.

Главная.код XAML:

<Window x_Class="example.Main"
        

        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"
      
      
       
       
      
      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 ответ

  1. Добавьте общедоступное свойство на Страницу1.код XAML.класс cs, возвращающий ссылку на текстовое поле:

    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }
    
        public TextBox TxtSearch
        {
            get { return txtSearch; }
        }
    }
    

    …и привязка к нему в вашем стиле в окне:

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