2016-12-29 41 views
0

我有一個簡單的問題。我嘗試了太多,並沒有解決。我有一個WPF窗口和一個頁面。點擊按鈕時,頁面顯示在主窗口的框架上。它用作主窗口上的按鈕DataTrigger,並希望在主窗口內的按鈕可以觸發,而另一頁的textbox則是關注的。如何訪問頁面的元素以在WPF中的另一個頁面文本框中進行綁定?

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> 
+0

你的問題不清楚?當page1中的文本框關注時,什麼應該改變背景顏色? –

+0

當頁面1中的文本框聚焦時,應在主窗口中的按鈕上更改背景顏色。 –

回答

0

的公共屬性添加到返回到TextBox的引用Page1.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"/> 
+0

爲什麼你使用Content「Path = Content.TxtSearch.IsFocused」?謝謝。 –

+0

因爲Frame的Content屬性返回帶有要綁定到的TextBox屬性的Page。 – mm8

+0

非常感謝。 –

相關問題