كيف يمكنني تغيير حجم الخط من تسمية في بلدي ControlTemplate

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

سؤال

في بلدي مربع القائمة WPF، ولدي الاسلوب مع ControlTemplate لListBoxItem. داخل هذا ControlTemplate لدي تسمية محددة. واستنادا إلى بعض التفاصيل، ولست بحاجة لتغيير حجم الخط التسمية. لذلك من وجهة نظري التعليمات البرمجية الخلفية، ولست بحاجة لتحديد ما يجب أن يكون الخط وبعد ذلك تحتاج إلى تعيينه.

وهنا هو أسلوبي مع ControlTemplate (لقد جردت من بعض الضوابط لا صلة لها بالموضوع)

<Style x:Key="RecordTabList" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="{DynamicResource RecordIndexTabBackcolor}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>        
                            <Label
                                x:Name="myLabel" Grid.Column="0" Grid.ColumnSpan="1" Grid.Row="0" Grid.RowSpan="1" Margin="3,-2,0,-2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Foreground="{DynamicResource RecordIndexTabForeground}" 
                                FontSize="10" Height="Auto" BorderThickness="3,0,0,0"
                                Content="{Binding Path=Name}" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

وكيف يمكنني أن أفعل هذا؟

هل كانت مفيدة؟

المحلول

إذا كنت فهمت بشكل صحيح، وربما يمكنك القيام بشيء مماثل لما يلي، وببساطة تغيير الخاصية حجم الخط على ListBoxItem ذاته؛ وسوف تنعكس تلقائيا على تسمية الخاص بك. نسخ هذا إلى VS وترى أنه في العمل!

<Window.Resources>
    <Style TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Label Content="{TemplateBinding Content}" FontSize="{TemplateBinding FontSize}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox Margin="12">
        <ListBoxItem Content="Test 1" FontSize="14"/>
        <ListBoxItem Content="Test 2" FontSize="18"/>
        <ListBoxItem Content="Test 3" FontSize="22"/>
    </ListBox>
</Grid>

نصائح أخرى

هل يمكن أن يكون قادرا على استخدام ValueConverter على حجم الخط الملكية .. لكنني لست متأكدا 100٪ إذا كانوا يعملون داخل ControlTemplate .. يبدو لي أن نتذكر سيلفرلايت تواجه مشاكل معها، ولكن لا أستطيع أن أتذكر ما اذا كان يعمل في برنامج الأغذية العالمي.

إذا كنت ترغب في تعيين حجم الخط في التعليمات البرمجية خلف، يجب إزالة حجم الخط من ControlTemplate، ثم تعيينها لListBoxItem في التعليمات البرمجية. إذا كنت ترغب في تعيين نفس الحجم لجميع ListBoxItems فقط تعيين حجم الخط في مربع القائمة في التعليمات البرمجية.

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