เชื่อมโยงกับ TemplatedParent จาก DataTemplate ที่กำหนดไว้ในสไตล์อย่างไร

ฉันกำลังพัฒนาการควบคุมแบบกำหนดเองที่ได้มาจาก ItemsControl ในไฟล์ generic.xaml ฉันได้สร้างสไตล์สำหรับการควบคุมนั้นและกำหนด ItemTemplate ด้วย:

<Style TargetType="local:MyItemsControl">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border Background="Red">
                    <!-- Other things in here -->
                </Border>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

ฉันต้องการผูกคุณสมบัติ Background ของ Border ใน DataTemplate กับคุณสมบัติการพึ่งพาของ MyItemsControl

หากพบคำถามหลายข้อที่นี่ แนะนำให้ใช้ชื่อองค์ประกอบของ MyItemsControl ในการโยง แต่จะใช้ได้เฉพาะเมื่อกำหนด ItemTemplate ที่ใช้ตัวควบคุมเท่านั้น ฉันยังลองเชื่อมโยงกับ RelativeSource ซึ่งกำหนด local:MyItemsControl เป็นประเภทบรรพบุรุษ

ไม่มีอะไรทำงาน ฉันพลาดอะไรไปที่นี่?


person Spontifixus    schedule 29.08.2013    source แหล่งที่มา
comment
ใน ControlTemplate คุณสามารถใช้ TemplateBinding ได้ แต่จะมีเฉพาะใน ControlTemplates... ฉันจะลองใช้วิธีบรรพบุรุษ   -  person MVCible    schedule 29.08.2013
comment
ฉันพยายามแล้ว - แต่ไม่สามารถใช้งานได้ วิธีนี้ใช้ไม่ได้ผล หรือฉันทำผิด...   -  person Spontifixus    schedule 29.08.2013


คำตอบ (1)


DependencyProperty ประเภทนั้นคืออะไร? มันคือ Brush หรือ string? รหัสง่ายๆนี้ใช้ได้กับฉัน:

Background="{Binding Name, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 

สำหรับการทดสอบที่นี่ ฉันผูกเข้ากับคุณสมบัติ Name ของ ItemsControl ที่เป็น 'Yellow' - และใช้งานได้

person icebat    schedule 30.08.2013