locked
Blend 1 and Visual Studio 2005 event handling (AdrianEdmonds) - 2/2/2008 11:26 PM PST RRS feed

  • General discussion

  • By: AdrianEdmonds


    Hi all;
    
    I'm trying to create a GUI in Blend with custom buttons. We don;t have 
    any problems with the basics but we're stuck with handling events via 
    code rather than XAMl.
    
    The problem.
    
    Create a grouped shape say with an outer ellipse and an inner. Group and 
    make a button. Add a click event and then launch into Visual Studio 
    2005. The event handler is there and you get get the source object with 
    a cast.
    
            private void EllipseButton_Click(object sender, RoutedEventArgs e)
             {
                 Button me = (Button)(sender);
    
    This button appears to be different from a normal button created in WPF. 
    For example, there is no Text function available.
    
    "Error	2	'System.Windows.Controls.Button' does not contain a 
    definition for 'Text' 
    D:\Projects\ExpressionBlend\AnatNotBlend2Test\Window1.xaml.cs	42"
    
    What we want to do is progamatically change the color of the inner 
    ellipse of this button and the text. Where can I find the inner ellipse 
    in code?
    
    Anyone any clues? Using Blend 1, WS2005 , XP SP2 etc.
    
    Regards
    
    Adrian Edmonds
    
    Friday, February 22, 2008 6:29 PM

All replies

  • By: Corrado Cavalli [MVP]


    Button is a contentcontrol so it exposes a Content property that represents
    what's inside the button, to get into it you have to walk its visual tree
    using VisualTreeHelper class.

    <Button HorizontalAlignment="Left" Margin="67,101,0,0"
    VerticalAlignment="Top" Width="144" Height="53" Click="Button_Click" >
    <Grid Width="144" Height="53">
    <Ellipse Fill="#FFFFFFFF" Stroke="#FF000000"/>
    <TextBlock Text="TextBlock" TextWrapping="Wrap"
    HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
    </Button>

    DependencyObject FindObject (DependencyObject source, Type type)
    {
    DependencyObject obj;
    int children = VisualTreeHelper.GetChildrenCount(source);
    for (int i = 0; i < children; i++)
    {
    obj = VisualTreeHelper.GetChild(source, i);
    if (obj.GetType() == type)
    return obj;
    else
    {
    obj =FindObject(obj, type);
    if (obj != null) return obj;
    }
    }
    return null;
    }

    private void Button_Click (object sender, RoutedEventArgs e)
    {
    Button btn = sender as Button;
    DependencyObject inside = btn.Content as DependencyObject;
    Ellipse obj1 = (Ellipse)this.FindObject(inside, typeof(Ellipse));
    TextBlock obj = (TextBlock)this.FindObject(inside, typeof(TextBlock));
    obj.Text = "hello";
    obj1.Fill = new SolidColorBrush(Colors.Red);

    }

    HTH

    --
    Corrado Cavalli [Microsoft .NET MVP-MCP]
    UGIdotNET -
    http://www.ugidotnet.org
    Weblog: http://blogs.ugidotnet.org/corrado/
    --
    Corrado Cavalli [Microsoft .NET MVP-MCP]
    UGIdotNET -
    http://www.ugidotnet.org
    Weblog: http://blogs.ugidotnet.org/corrado/
    "AdrianEdmonds" <adrian.edmonds@stryker.com> wrote in message
    news:%23qoUGXjZIHA.1208@TK2MSFTNGP03.phx.gbl...
    > Hi all;
    >
    > I'm trying to create a GUI in Blend with custom buttons. We don;t have any
    > problems with the basics but we're stuck with handling events via code
    > rather than XAMl.
    >
    > The problem.
    >
    > Create a grouped shape say with an outer ellipse and an inner. Group and
    > make a button. Add a click event and then launch into Visual Studio 2005.
    > The event handler is there and you get get the source object with a cast.
    >
    > private void EllipseButton_Click(object sender, RoutedEventArgs e)
    > {
    > Button me = (Button)(sender);
    >
    > This button appears to be different from a normal button created in WPF.
    > For example, there is no Text function available.
    >
    > "Error 2 'System.Windows.Controls.Button' does not contain a definition
    > for 'Text' D:\Projects\ExpressionBlend\AnatNotBlend2Test\Window1.xaml.cs
    > 42"
    >
    > What we want to do is progamatically change the color of the inner ellipse
    > of this button and the text. Where can I find the inner ellipse in code?
    >
    > Anyone any clues? Using Blend 1, WS2005 , XP SP2 etc.
    >
    > Regards
    >
    > Adrian Edmonds
    Monday, February 25, 2008 6:34 PM
  • By: AdrianEdmonds


    Thanks you Corrado. That worked a treat.
    
    Adrian
    
    Corrado Cavalli [MVP] wrote:
    > Button is a contentcontrol so it exposes a Content property that 
    > 
    >  SNIP
    > 
    > HTH
    > 
    
    Monday, February 25, 2008 6:40 PM