简体   繁体   中英

How to access Textblock in DataTemplate in ribbon in WPF?

I am new using WPF I search whole day but I did not find any answers that I can apply it to may application.

I have a Menu ribbon but i want to set the title of the ribbon through my .cs code. My problem is I cannot access the textblock inside my <Ribbon.TitleTemplate> .

Here's my XAML code

                <Ribbon x:Name="Ribbon">
                    <Ribbon.TitleTemplate>
                        <DataTemplate >
                            <TextBlock x:Name="FrontPageTitle" TextAlignment="Center" HorizontalAlignment="Stretch" Width="{Binding ElementName=RibbonWindow, Path=ActualWidth}">
                                <TextBlock.Effect>
                                    <DropShadowEffect ShadowDepth="0" Color="MintCream " BlurRadius="10"   />
                                </TextBlock.Effect>
                            </TextBlock> 
                        </DataTemplate>
                    </Ribbon.TitleTemplate>
                    <Ribbon.HelpPaneContent>
                        <RibbonButton SmallImageSource="pack://application:,,,/MYSEP;component/Images/window.png" />
                    </Ribbon.HelpPaneContent>
                    <Ribbon.QuickAccessToolBar>
                        <RibbonQuickAccessToolBar>
                            <RibbonButton x:Name="QATButton1" 
                                         SmallImageSource="pack://application:,,,/MYSEP;component/Images/window.png" />
                            <RibbonButton x:Name="QATButton2" 
                                         SmallImageSource="pack://application:,,,/MYSEP;component/Images/window.png" />
                        </RibbonQuickAccessToolBar>
                    </Ribbon.QuickAccessToolBar>
                </Ribbon>

And Here's my C# code

public FrontPage()
        {
            InitializeComponent();

            mypSepProject = (Project)App.Current.Properties["myp_projects"];

            FrontPageTitle.Text = mypSepProject.ProjectName;
            CreateRecentMenu();
            SetTreeViewMenu();
            ShowChildeWindow("Process Data");
        }

But FrontPageTitle does not exist in the current page. But when I put the textblock outside the DataTemplate , it can read the FrontPageTitle already. I hope you can help me guys. Thank you.

You have to use data binding. For this purpose, add a DepndencyProperty to the FrontPage class eg, Title and bind the TextBlock.Text of the Ribbon.TitleTemplate to it.
In case FrontPage is a Page , you can use the inherited Page.Title property instead:

FrontPage.xaml.cs

public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
  "Title",
  typeof(string),
  typeof(FrontPage),
  new PropertyMetadata(default(string)));

public string Title
{
  get => (string) GetValue(FrontPage.TitleProperty);
  set => SetValue(FrontPage.TitleProperty, value);
}

public FrontPage()
{
  InitializeComponent();

  mypSepProject = (Project)App.Current.Properties["myp_projects"];

  this.Title = mypSepProject.ProjectName;
}

FrontPage.xaml

<Ribbon x:Name="Ribbon">
  <Ribbon.TitleTemplate>
    <DataTemplate>
      <TextBlock x:Name="FrontPageTitle" 
                 Text="{Binding RelativeSource={RelativeSource AncestorType=FrontPage}, Path=Title}"" />
    </DataTemplate>
  </Ribbon.TitleTemplate>
</Ribbon>

I am half an hour late to the party:-)

Add this to your FrontPageTitle TextBlock :

Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Ribbon}}, Path=Title}"

And in the code you can just do:

Ribbon.Title = "ABC";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM