WPF에서는 ItemsControl ControlTemplate에서 이벤트를 어떻게 처리 할 수 ​​있습니까?

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

문제

ItemsControl ControlTemplate 내부의 이벤트를 처리하려고합니다. 나는 버튼의 마우스 업 및 마우스 딩 이벤트를 할당했습니다 (아래 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>
도움이 되었습니까?

해결책

버튼 클릭 이벤트를 사용하는 대신 새 명령을 작성하고 버튼의 명령 속성을 생성 한 명령에 바인딩 한 다음 명령 바인딩을 사용자 컨트롤에 추가하여 실행될 때 명령을 처리합니다.

보다 여기 ~을 위한 추가 정보.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top