0

I have about 50 different pieces of data that I need to call, for minimum and maximum values. I'm sure there's a better way than doing the following:

 public void refreshminmaxvalues(List<minmaxvalues> listminmaxvalues)
    {
        txtMinA_1.Text = minmaxvalueslist[0].minvalue.ToString();
        txtMaxA_1.Text = minmaxvalueslist[0].maxvalue.ToString();
        txtMinB_1.Text = minmaxvalueslist[0].minvalueB.ToString();
        txtMaxB_1.Text = minmaxvalueslist[0].maxvalueB.ToString();
        txtMin_2.Text = minmaxvalueslist[1].minvalue.ToString();
        txtMax_2.Text = minmaxvalueslist[1].maxvalue.ToString();

    }

The above code takes values from a list for min and max. The MinA and MinB are two values that are linked together, and thus will be called differently from the rest. Everything past txtMax_2 will just have one min and one max value.

Does anyone know a better, more "efficient" way of doing this code, rather than just writing out all 50+ values?

If it helps, these are the values that are edited in XAML:

          <!-- Minimum -->
        <TextBox Name="txtMinA_1" Text=""/>
        <TextBox Name="txtMinB_1" Text=""/>
        <TextBox Name="txtMin_2" Text=""/>
        <TextBox Name="txtMin_3" Text=""/>
        <TextBox Name="txtMin_4" Text=""/>

         <!-- Maximum -->
        <TextBox Name="txtMaxA_1" Text=""/>
        <TextBox Name="txtMaxB_1" Text=""/>
        <TextBox Name="txtMax_2" Text=""/>
        <TextBox Name="txtMax_3" Text=""/>
        <TextBox Name="txtMaxH_4" Text=""/>

2 Answers 2

1

If you know exactly how many pieces of data are being returned then you should use models and databinding instead.

For example:

YourXaml.xaml

EDIT: use the ItemsTemplateSelector in DLeh's answer but the other stuff in mine still makes sense and is relavent.

 <TextBox Name="txtMinA_1" Text="{Binding txtMinA_1.minvalue"/>
 <TextBox Name="txtMinA_1" Text="{Binding txtMinB_1.maxvalue"/>
 ect...

YourXaml.xaml.cs

public class YourXaml{
     public YourXaml(){
        this.DataContext = new YourViewModel();
     }
}

YourViewModel.cs

public YourViewModel{
    private minmaxvalues _txtMinA_1;
    private minmaxvalues _txtMinB_1;

    public minmaxvalues txtMinA_1
    { 
       get{ return _txtMinA_1; }
       set{ _txtMinA_1 = value; }
    }
    public minmaxvalues txtMinB_1
    { 
       get{ return _txtMinB_1; }
       set{ _txtMinB_1= value; }
    }

}

By using this structure you are decoupling your view (YourXaml.xaml) from your data which is in the models and viewmodel. It's a much more efficient and industry accepted way of working in WPF.

To learn more about MVVM go here. You are going to want to learn this along with what DataBinding is.

Sign up to request clarification or add additional context in comments.

Comments

1

You should be able to use an ItemsControl and bind to that:

<ItemsControl x:Name="Mins">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding minvalue}"></TextBox>
            <TextBox Text="{Binding minvalueB}"></TextBox>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl x:Name="Maxes">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding maxvalue}"></TextBox>
            <TextBox Text="{Binding maxvalueB}"></TextBox>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

You could bind a type to it like so:

Mins.ItemsSource = listminmaxvalues;
Maxes.ItemsSource = listminmaxvalues;

More info: WPF Repeater (like) control for collection source?

7 Comments

I like your answer better than mine however the OP needs to know what databinding is
which he should if he's going to develop in WPF, but that's a bit out of scope for this question.
I don't think it is. Both of our answer use databinding where in his original question he isn't at all. If he implements your answer it won't work. It's not a complete solution.
Since i'm setting the ItemsSource directly in the codebehind instead of doing it with a view model as a parent DataContext, this should be a complete solution.
@Badja if it's hard for you to add databinding with the code that you're writing then you need to re-evaluate your architecture. I write EXTREMELY complex WPF applications and the more complex they get, the better Databinding works.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.