Reputation: 357
I have frames in my main window which are set to visible/collapsed based on user input:
<Grid>
<ScrollViewer x:Name="ScrollViewer1" Grid.Row="1" Grid.ColumnSpan="3" Margin="10,0,0,0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<Frame Name="InputsFrame" Source="Inputs.xaml" NavigationUIVisibility="Hidden" Visibility="Visible"
ScrollViewer.CanContentScroll="True" />
</ScrollViewer>
<ScrollViewer x:Name="ScrollViewer2" Grid.Row="1" Grid.ColumnSpan="3" Margin="10,0,0,0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Visibility="Collapsed">
<Frame Name="LoadCasesFrame" Source="LoadCases.xaml" NavigationUIVisibility="Hidden" Visibility="Collapsed"
ScrollViewer.CanContentScroll="True" />
</ScrollViewer>
<!-- etc -->
</Grid>
The Inputs.xaml frame basically just consists of a 3rd party DoubleTextBox control (over 100 of them), and the user can just enter in values to that page. C# code behind:
private void InputsTab_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
LoadCasesFrame.Visibility = Visibility.Collapsed;
ScrollViewer2.Visibility = Visibility.Collapsed;
InputsFrame.Visibility = Visibility.Visible;
ScrollViewer1.Visibility = Visibility.Visible;
}
In this main window, there is a menu on top to allow for saving and opening the file. When I open the file, I want the data to be read (which I am able to do successfully) and also for the UI in the Inputs.xaml file to be updated.
The following code is in Inputs.xaml.cs:
public void LoadValues()
{
List<DoubleTextBox> dtb1 = App.GetLogicalChildCollection<DoubleTextBox>(inputsGrid);
for (int i = 0; i < dtb1.Count; i++)
{
foreach (var keyValuePair in App.globalDictionary)
{
var doubleTextBox = dtb1[i] as DoubleTextBox;
if (doubleTextBox.Name == keyValuePair.Key)
{
doubleTextBox.Value = 500;
break;
}
}
}
}
This function works (all the values in the GUI update to 500) when I call it from the Inputs.xml.cs page (for example, when I put it in the Page_Loaded
event). However, I need to call this function from the MainWindow
, since that is where the event handler for the Open File event is located:
private void openProject_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
string stringToDeserialize = File.ReadAllText(openFileDialog.FileName);
App.DeserializeJSONString(stringToDeserialize);
}
// call LoadValues here
}
Calling LoadValues()
above doesn't update the GUI in the Input.xaml page. I originally had something like this in my MainWindow
:
Inputs _inputs = new Inputs();
_inputs.LoadValues();
I know that the problem is that I have created a new object for Inputs
and that's probably why it's not working. I'm unsure how to do it so that I don't use a new object -- wonder if I could use the InputsFrame
somehow. I've also tried using event handlers to no success.
Upvotes: 3
Views: 2246
Reputation: 70701
If you want to interact with the Inputs
object that is in your Frame
element, you need to retrieve that one, not create a new one.
You can do that like this:
((Inputs)InputsFrame.Content).LoadValues();
I.e. the Contents
property of the Frame
returns a reference to the Inputs
object that is used to populate the Frame
. Just cast that to Inputs
to access the appropriate members of that class, such as LoadValues()
.
Upvotes: 2