في WPF، كيف يمكنني التعامل مع هذا الحدث في ControlTemplate ItemsControl

StackOverflow https://stackoverflow.com/questions/1408065

سؤال

وأنا أحاول التعامل مع حدث داخل ControlTemplate ItemsControl. لقد أوليت أحداث MouseUp وMouseDown على زر (btnRight أدناه). المشكلة هي أنه عندما كنت اضغط على الزر، الحدث لم يصل لي رمز وراء. كيف الأحداث في العمل ControlTemplates وماذا يجب أن أفعل لربط هذا الأمر؟ لقد حاولت تعيين الأحداث إلى زر في التعليمات البرمجية الخلفية خلال هذا الحدث OnApplyTemplate دون جدوى.

وشكرا لمساعدتكم!

<ItemsControl.Template>
    <ControlTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="36" />
                <ColumnDefinition />
                <ColumnDefinition Width="36" />
            </Grid.ColumnDefinitions>
            <Button x:Name="btnLeft" Grid.Column="0" Height="36">
                <Button.Template>
                    <ControlTemplate>
                        <Image>
                            <Image.Source>
                                <BitmapImage UriSource="Images\left.png" />
                            </Image.Source>
                        </Image>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Border Grid.Column="1" BorderBrush="Black" BorderThickness="1" Background="Black" Padding="6">
                <ItemsPresenter Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MarginOffset}" />
            </Border>
            <Button x:Name="btnRight" Grid.Column="2" Height="36" MouseUp="btnRight_MouseUp" MouseDown="btnRight_MouseDown">
                <Button.Template>
                    <ControlTemplate>
                        <Image>
                            <Image.Source>
                                <BitmapImage UriSource="Images\right.png" />
                            </Image.Source>
                        </Image>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </Grid>
    </ControlTemplate>
</ItemsControl.Template>
هل كانت مفيدة؟

المحلول

وبدلا من استخدام انقر على زر الأحداث، إنشاء قيادة جديدة، ربط خاصية قيادة زر للقيادة قمت بإنشائه، ثم قم بإضافة CommandBinding إلى التحكم الخاصة بك لمعالجة الأمر عند تنفيذه.

هنا <أ href ل = "http://www.dev102.com/2008/12/10/using-command-binding-to-enhance-any-wpf-control/" يختلط = "نوفولو noreferrer"> ل مزيد من المعلومات.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top