Kippachu
❔ Need help with WPF Prism app
Then, within the OnPreviewContentPropertyChanged method, I have to use a switch statement based on the key value and update the corresponding property in the PreviewTextService dictionary. This means that every time I create a new form implementing Preview Text, I have to manually add all cases inside the switch statement. I am unsure how to replace this code with a more efficient pattern. If anyone could help me improve my code, I would greatly appreciate it!
5 replies
Problem with my project
There are many ways of doing this, the simplest way would be to create a private field inside the class (outside the methods)
Change timeOfFlight to _timeOfFlight inside NoHorizontalAccelerationCalculation() method this way you are setting the value outside your method and so it becomes usable in others methods of the same class (private field)
This means now you can do
Note : This will work but is totally not the best way to do, you'll have to change this behaviour later., just gave you an easy solution in many solutions.
Is it ok for you ?
3 replies
❔ Illuminate buttons pressing a row of a DataGrid
Basically when you select a row then your variable "selectedPerson" in DataGrid_SelectedCellsChanged will not be null, so inside UpdateButtonColors the else case will be run, changing the color of every people in your MyPeopleList, and as you binded the buttons colors to your people color, it will also change, else if unselect the row the selectedPerson will be null and will set the buttons color back to grey
20 replies
❔ Illuminate buttons pressing a row of a DataGrid
You'll need to implement INotifyPropertyChanged inside People class then add :
In MainWindow.xaml bind Button Background to ButtonColor property, also in DataGrid properties, add SelectedCellsChanged and create the method DataGrid_SelectedCellsChanged
Like this :
<DataGrid Margin="20,5,20,5"
SelectedCellsChanged="DataGrid_SelectedCellsChanged"
Height="200" Width="600" ItemsSource="{Binding MyListOfPeopleForBinding}" AutoGenerateColumns="False">
You have to create this method inside MainWindow.xaml.cs
private void DataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (sender is not DataGrid dataGrid) return;
var vm = DataContext as MainViewModel;
var selectedPerson = dataGrid.SelectedItem as People;
vm?.UpdateButtonColors(selectedPerson);
}
Add the method UpdateButtonColors(People selectedPerson) in your viewModel or it will not work :
public void UpdateButtonColors(People selectedPeople)
{
foreach (var person in MyListOfPeople)
{
if (selectedPeople == null)
{
person.ButtonColor = Brushes.Gray; // Default color
}
else
{
person.ButtonColor = person.IsOk ? Brushes.Green : Brushes.Red;
}
}
}
20 replies
✅ Does arithmetic in a `const float` gets recomputed each time you access it?
When you declare a const float and initialize it with a value or an arithmetic expression, the value is computed at compile-time, and it remains constant throughout the program. The compiler treats constant expressions as fixed values, and it may optimize the code further by replacing the constant variable with its value directly in the compiled code.
Here's an example:
In this example, doublePi will be calculated at compile-time, and its value will be stored as a constant. Each time you access doublePi, the value will not be recomputed – it will simply return the precomputed constant value.
6 replies