3
我在接口有一個自定義的按鈕,用下面的樣式定義裏面:獲取的ScrollViewer自定義風格的按鈕
<Style x:Key="KinectCustomButton" TargetType="k:KinectCircleButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="k:KinectCircleButton">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60*"/>
<RowDefinition Height="40*"/>
</Grid.RowDefinitions>
<k:KinectCircleButton Grid.Row="0" VerticalAlignment="Bottom" Foreground="{TemplateBinding Foreground}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
<ContentPresenter x:Name="content"/>
</k:KinectCircleButton>
<ScrollViewer Grid.Row="1">
<TextBlock TextAlignment="Center" VerticalAlignment="Top" TextWrapping="Wrap" Text="{TemplateBinding Label}" Foreground="{TemplateBinding Foreground}" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontWeight="{TemplateBinding FontWeight}"/>
</ScrollViewer>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我在窗口實例化這些按鈕中的六個。現在我需要爲這些按鈕中的每一個訪問ScrollViewer元素。 我試過這種方法:How can I find WPF controls by name or type?但它不工作。我也嘗試訪問我自定義的KinectCustomButton的Template屬性,但是如果我試圖找到ScrollViewer實例,我會從模板中獲取一個,而不是按鈕實例中的一個(所以TextBlock裏面的文本是空的)。有什麼方法可以獲得我想要的嗎?
'現在我需要爲這些按鈕中的每一個訪問ScrollViewer元素 - 爲什麼?不要在程序代碼中操作UI元素。 –
我在這裏做的是在我的scrollviewer元素內部實現自動滾動,所以我需要獲得對它們的引用以應用我的自動滾動方法。如果你的意思是這樣做會阻止UI線程(或類似的東西),我已經照顧它了 – breathe0