2

I'm just starting out with WPF having used WinForms for some time and seem to have fallen at the first hurdle.

I have my main XAMLdefined as

  <Window x:Class="FHIRCDALoader.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:FHIRCDALoader.xaml"
        Title="FHIR CDA Loader" Height="350" Width="525"
        Icon="Icons/color_swatch.png">

    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.New"
                        Executed="NewDocument" />
    </Window.CommandBindings>

    <DockPanel>
        <local:menubar  DockPanel.Dock="Top"/>
        <local:toolbar  DockPanel.Dock="Top"/>

        <local:statusbar DockPanel.Dock="Bottom" />

        <RichTextBox x:Name="Body"/>

    </DockPanel>


</Window>

Note the use of the user controls, one of which is the "statusbar"

<UserControl x:Class="FHIRCDALoader.xaml.statusbar"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StatusBar >
        <StatusBarItem>
            <TextBlock x:Name="bbstatusbar" />
        </StatusBarItem>
    </StatusBar>
</UserControl>

So in MainWindow.xaml.cs I see I can reference RichTextBox named body from the main XAML file. I can't however reference the TextBlock in the UserControl which is named "bbstatusbar".

How do I set the value of the TextBlock from MainWindow.xaml.cs?

BENBUN Coder
  • 4,801
  • 7
  • 52
  • 89
  • 2
    The usually preferred way is to use MVVM: (1) you define view models for your main window and control, (2) you bind the value of textbox's text to the appropriate dependency property in the usercontrol's view model, (3) you access the bound property from the main view model. – Vlad Nov 27 '13 at 23:10
  • 1
    Learn MVVM. Please read [this](http://stackoverflow.com/a/15684569/643085) carefully. You don't "access" the UI in WPF to retrieve / set data, simply because [UI is not Data](http://stackoverflow.com/a/14382137/643085) – Federico Berasategui Nov 27 '13 at 23:11

1 Answers1

3

In agreement with Vlad and HighCore's comments: you don't set the TextBlock from MainWindow.xaml.cs. You bind it to a view-model. A binding simply looks like this:

<TextBlock Text="{Binding StatusText}" />

The above says: bind the Text property to a property in the current data-context called "StatusText". Next, create a view model:

public class ViewModel : INotifyPropertyChanged
{
    public string StatusText
    {
        get { return _statusText; }
        set
        {
            _statusText = value;
            RaisePropertyChanged("StatusText");
        }
    }

    // TODO implement INotifyPropertyChanged
} 

Finally, set the DataContext of your MainPage to the view model. You can do this in a variety of ways, but let's say here for simplicity, do it in the constructor:

public MainWindow()
{
    InitializeComponent();
    DataContext = new ViewModel { StatusText = "hello world" };
}

Now, the idea is to put your model-related logic into ViewModel. So, you shouldn't need to access the UI elements -- instead, update the view-model properties that the UI elements are bound to.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260