Quantcast
Channel: Caliburn.Micro: Xaml Made Easy
Viewing all articles
Browse latest Browse all 1760

New Post: Convention binds to child button in ControlTemplate (bug?)

$
0
0

Hello,

I run into the following issue today and I am wondering if this is a bug or if I missed something.

I implemented a SplitButton control:

    [TemplatePart(Name = "PART_MainButton", Type = typeof(Button))]
    [TemplatePart(Name = "PART_DropDownButton", Type = typeof(ToggleButton))]publicclass SplitButton : ButtonBase
    {

I map the MainButton_Click to the control's Click event:

var mainButton = GetMainButton();if (mainButton != null)
                mainButton.Click += mainButton_Click;

... and ...

void mainButton_Click(object sender, RoutedEventArgs e)
        {
            OnClick();
        }
The ControlTemplate looks like this:

<StyleTargetType="{x:Type tsc:SplitButton}"><SetterProperty="Template"><Setter.Value><ControlTemplateTargetType="{x:Type tsc:SplitButton}"><Grid><Grid.ColumnDefinitions><ColumnDefinitionWidth="*"/><ColumnDefinitionWidth="Auto"/></Grid.ColumnDefinitions><ButtonName="PART_MainButton"Grid.Column="0"><ContentPresenterContent="{TemplateBinding Content}"/></Button><ToggleButtonName="PART_DropDownButton"Grid.Column="1"Padding="3"><PathName="dropDownArrow"Fill="{DynamicResource DropDownArrowBrushSelected}"Data="M 0 0 L 4 4 L 8 0 Z"Height="5"Margin="0"/></ToggleButton></Grid></ControlTemplate></Setter.Value></Setter></Style>

Ok, so far it works.

Now I use my control in a View:

<local:SplitButtonName="ExportTest"Content="Export">

and declare a method in my ViewModel:

publicvoid ExportTest()
        {var t = "Test";
            System.Diagnostics.Debug.WriteLine(t);
        }

The problem is that the ExportTest() method is called twice when I click on the MainButton and once when I click on the DropDownButton. In the first case it should only be called once and in the second case it should not be called at all.

Further tests revealed that Caliburn.Micro is binding the ExportTest() method to the two buttons within the ControlTemplate. Very strange.... Even if CM applied conventions to the controls within the ControlTemplate - and I am not sure if it should or should not do it - the buttons within the ControlTemplate have Names as well and I do not understand why they are bound to a method with their parent's name?

Any idea?

BTW, thanks for the great framework! - I used Microsoft Prism before, than Prism with Caliburn.Micro together and then switched to Caliburn.Micro all together and I am not looking back. I am happy that Caliburn.Micro is maintained and has a great future where as Prism seems like a dead end.

Best regards,

Peter

 

 


Viewing all articles
Browse latest Browse all 1760

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>