1

Have a WPF UserControl with a DockPanel containing a DevExpress GridControl and, following and outside of the GridControl, a TextBox:

<UserControl x:Class="HellerOven.Setup.SetupGUI.WatlowView"
         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:resx="clr-namespace:HellerOven.Languages"
         xmlns:local="clr-namespace:HellerOven.Setup.SetupGUI"
         xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
         xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
         xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
         xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<DockPanel>
    <dxg:GridControl x:Name="WatlowSensorsGrid" EnableSmartColumnsGeneration="True" DockPanel.Dock="Top" ItemsSource="{Binding}" FontSize="14" FontWeight="Medium"  Height="600" Margin="0,20,0,0">
    ...
    </dxg:GridControl>

    <TextBox Text="Alarm activation delay [seconds]" IsReadOnly="True" FontSize="14" Foreground="RoyalBlue" FontWeight="Medium" BorderThickness="0" Height="30"/>
    <TextBox Text="30" Name="alarmActivationDelay" FontSize="14" Foreground="RoyalBlue" FontWeight="Medium" BorderThickness="1" Width="100" Height="30"/>

    <Button Height="35" Width="200" Click="Add_Button_Click" HorizontalAlignment="Center" DockPanel.Dock="Bottom" Margin="0,50,0,0">
        <StackPanel Orientation="Horizontal">
            <Image Source="{dx:DXImage Image=Add_32x32.png}" Stretch="Uniform"/>
            <TextBlock Text="Add Watlow Sensor" VerticalAlignment="Center" Margin="5 0 0 0" FontSize="14"/>
        </StackPanel>
    </Button>
</DockPanel>

In the C# constructor for the UserControl, I specify separate DataContext for the GridControl and for the TextBox:

    public WatlowView()
    {
        InitializeComponent();
        WatlowSensorsGrid.DataContext = OvenCollections.Instance.SetupWizard.WatlowSensors;
        alarmActivationDelay.DataContext = OvenCollections.Instance.SetupWizard.alarmActivationDelay;
    }

During program execution, the GridControl and its members are updated in its assigned DataContext, but the simple integer "alarmActivationDelay" is not updated in its assigned DataContext. What am I misunderstanding regarding DataContext? Thanks.

2 Answers2

0

To see changes in Gridcontrol can use this

WatlowSensorsGrid.BeginDataUpdate();
WatlowSensorsGrid.ItemsSource = OvenCollections.Instance.SetupWizard.WatlowSensors;
WatlowSensorsGrid.RefreshData();
WatlowSensorsGrid.EndDataUpdate();

For textbox , you can simply set alarmActivationDelay.Text but if you you want to use dataContext , you must implement INotifyPropertyChanged interface. You can see implementation at this link

AmirHossein Rezaei
  • 1,086
  • 1
  • 16
  • 20
  • Thank you but I've decided to use the answer by user3308241 above. Thanks for the suggestion. –  Feb 06 '19 at 14:08
0

You have not set up a binding for the Text property of the TextBox, like you did with the Grid. Ideally, you should set the DataContext of the Window, then use Path= to set the property that each control binds to in the XAML. You also don't need to name each control if you do it this way. Here is what the code should look like:

    public WatlowView()
{
    InitializeComponent();
    this.DataContext = OvenCollections.Instance.SetupWizard;
    // No longer needed
    // WatlowSensorsGrid.DataContext = OvenCollections.Instance.SetupWizard.WatlowSensors;
    // alarmActivationDelay.DataContext = OvenCollections.Instance.SetupWizard.alarmActivationDelay;
}

Here is the related XAML, with the bindings set for both the grid and the textbox:

<DockPanel>
<dxg:GridControl x:Name="WatlowSensorsGrid" EnableSmartColumnsGeneration="True" DockPanel.Dock="Top" ItemsSource="{Binding Path=WatlowSensors}" FontSize="14" FontWeight="Medium"  Height="600" Margin="0,20,0,0">
...
</dxg:GridControl>

<TextBox Text="Alarm activation delay [seconds]" IsReadOnly="True" FontSize="14" Foreground="RoyalBlue" FontWeight="Medium" BorderThickness="0" Height="30"/>
<TextBox Text="{Binding Path=alarmActivationDelay}" Name="alarmActivationDelay" FontSize="14" Foreground="RoyalBlue" FontWeight="Medium" BorderThickness="1" Width="100" Height="30"/>

<Button Height="35" Width="200" Click="Add_Button_Click" HorizontalAlignment="Center" DockPanel.Dock="Bottom" Margin="0,50,0,0">
    <StackPanel Orientation="Horizontal">
        <Image Source="{dx:DXImage Image=Add_32x32.png}" Stretch="Uniform"/>
        <TextBlock Text="Add Watlow Sensor" VerticalAlignment="Center" Margin="5 0 0 0" FontSize="14"/>
    </StackPanel>
</Button>

user3308241
  • 344
  • 3
  • 10
  • 1
    Success! I had sensed I needed the TextBox Binding clause but not the constructor DataContext assignment that you show above. Once I did that, it works! Many thanks! –  Feb 06 '19 at 14:06
  • You're welcome. I'm glad this solution worked for you. – user3308241 Feb 07 '19 at 23:17
  • I think that there is a way that you can mark this as the accepted answer so that a green checkbox shows next to it. If you could do that, I'd really appreciate it. https://stackoverflow.com/tour – user3308241 Feb 07 '19 at 23:23