我現在擁有的一切:觸發設置控件的屬性在控件模板
<Style TargetType="{x:Type ListBox}" x:Key="PhotoListBoxStyle">
<Style.Triggers>
<Trigger Property="{Binding Path=IsChecked, ElementName=DetailView}" Value="False">
<Setter TargetName="WrapPanelItem" Property="ItemHeight" Value="100" />
<Setter TargetName="WrapPanelItem" Property="ItemWidth" Value="400" />
</Trigger>
</Style.Triggers>
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}" >
<WrapPanel Name="WrapPanelItem" Margin="5" IsItemsHost="True" Orientation="Horizontal"
ItemHeight="{Binding Value, ElementName=ZoomSlider }"
ItemWidth="{Binding Value, ElementName=ZoomSlider }"
VerticalAlignment="Top" HorizontalAlignment="Stretch" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我得到的最接近是名稱添加到WrapPanel嘗試和它上面的觸發參考,但無法找到目標名稱。我也嘗試創建連接到直接WrapPanel一個單獨的樣式,而是說使用這是它的父的PhotoListBoxStyle造成與控制問題:
<Style x:Key="WrapPanelSetter" TargetType="{x:Type WrapPanel}">
<Setter Property="ItemHeight" Value="{Binding Value, ElementName=ZoomSlider }" />
<Setter Property="ItemWidth" Value="{Binding Value, ElementName=ZoomSlider }" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="Margin" Value="5" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsChecked, ElementName=DetailView}" Value="False">
<Setter Property="ItemHeight" Value="100" />
<Setter Property="ItemWidth" Value="400" />
</DataTrigger>
</Style.Triggers>
</Style>
感謝您的幫助。如果有更好的選擇,我更願意嘗試不同的方法!
我實際上在我的項目中有這樣的錯誤,不確定舊版本是如何得到的 - 但這並不影響手頭的問題。如果我正確創建了該樣式,我可以將其附加到WrapPanel,但是然後引用主樣式PhotoListBoxStyle的控件將無法工作,並且說無法將字符串轉換爲system.windows.style。 – elbweb
@elbweb通常我會看到那個錯誤,如果我忘記指定我的Style是一個StaticResource。例如,有時我會寫'Style =「MyStyle」'而不是'Style =「{StaticResource MyStyle}」'。沒有理由爲什麼設置''WrapPanel Style =「...」/>'會影響你的'PhotoListBoxStyle'。也許你可以發佈完整的非工作代碼? – Rachel
我實際上找到了一個解決方案,而不必創建另一種風格。我可以在有問題的ControlTemplate中添加一個觸發器,並像我最初嘗試的那樣通過TargetName引用想要更改的控制器。我將這添加爲我的問題的解決方案。 – elbweb