C # why null after casting

Hello everyone, I salute me, I am so confused why my code is zero after casting this is xaml code, I have

<Window.Resources>
    <Style x:Key="Menu" TargetType="{x:Type Border}">
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Width" Value="25" />
        <EventSetter Event="MouseLeftButtonUp" Handler="Menu_MouseLeftButtonUp" />
    </Style>
</Window.Resources>

<Grid>
    <Border Name="BorderCloseWindow" CornerRadius="0,8,0,0" 
            Style="{StaticResource Menu}">
        <Image Source="pack://application:,,,/images/icons/CloseSTD.png" />
    </Border>
</Grid>

and this is C # that handle the border

private void Menu_MouseLeftButtonUp(object sender, RoutedEventArgs e)
{
    Border b = e.Source as Border;
    if (b.Name == "BorderCloseWindow")
    {
        this.Close();
    }
}

and if I click the mouse button on the border, which will give an error similar to this, the object instance does not have a reference to the object. which occur in

if(b.Name == "BorderCloseWindow")

Please help me, why does this give null? and how to restore my program so that you can work.

+3
source share
6 answers

It seems that e.Source is not a border, so e.Source as Border is NULL. The source may be another object within the border, with an event directed towards the border.

you can try checking the e.Source type with

if (e.Source is Border)
{
}

, e.Source.

+2

-, e.Source Border.

, - .

Border b = e.Source as Border;

null, e.Source , NullReferenceException. null , :

Border b = (Border)e.Source;

, ,

  • (InvalidCastException, NullReferenceException)
  • , ( if, ).

, -, : RoutedEventsArgs.Source - , (.. ). , , , . . RoutedEventArgs.Source.

, , e.Source:

Border b = (Border)sender;
+14

, Border, :

  Border b = sender as Border;

, , , sender e.Source.

+4

-, as , . , , .

Border b = (Border)e.Source;

InvalidCastException.

-, e.Source , , Image. , , sender.

, :

private void Menu_MouseLeftButtonUp(object sender, RoutedEventArgs e)
{
    Border b = (Border)sender;
    if (b.Name == "BorderCloseWindow")
    {
        this.Close();
    }
}

,

private void Menu_MouseLeftButtonUp(object sender, RoutedEventArgs e)
{
    this.Close();
}

, Control, - , -. : , Border ( ), XAML, #, , .

+4

:

private void Menu_MouseLeftButtonUp(object sender, RoutedEventArgs e)        
{            
  System.Diagnostics.Debug.WriteLine(
    "Sender contains an object of type {0}", 
    sender.GetType());
  System.Diagnostics.Debug.WriteLine(
    "e.Source contains an object of type {0}", 
    e.Source.GetType());
}

When the event is triggered, the necessary information will be written to the output window. Perhaps you need to make this visible using the View menu in Visual Studio. Then you can see what is happening and understand yourself.

+1
source

e.Source is a type image object.

So, you need to give the sender as a Border.

Border b = sender as a border;

+1
source

All Articles