简体   繁体   中英

Access to Usercontrols in WPF

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?

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.

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