So, what is better to use?

bool parsed = int.TryParse(string, out num);
if (parsed)


try {
do something…

Well, better is subjective!
If you don’t care to know why your int.TryParse fail, then use it.

However, int.Parse can (according to the documentation) throw three different exceptions:

the input is null
the input is not in a valid format
the input contains a number that procudes an overflow

If you care about why it fails, then int.Parse is clearly the better choice.


The problem is: How to bind a Command in a button, inside a datatemplate of a GridViewColumn ??
Like that:

<ListView ItemsSource="{Binding Lista}">
                        <Button Content="MyButton" Height="23" Width="75" Command="{Binding MyCommand}" CommandParameter="myParameter" />

When you are binding inside a list, the “datacontext” for the bindings is the object in the Itemsource, so if you bind a command, it will try to find the command inside the itemssource object.

So, how to bind the command “outside” the itemssource?

First of, you’ve gotta name your UserControl or Window:

<UserControl x:Name=”myControl” ….

So you’ve gotta set the binding of the command to:

Command=”{Binding Path=DataContext.myCommand, ElementName=myControl}”

This way, your binding will be for the Datacontext of MyControl.