【问题标题】:How to add CheckBox content from resource file in wpf?如何从 wpf 中的资源文件添加 CheckBox 内容?
【发布时间】:2015-10-30 18:42:13
【问题描述】:

我有一个组合框,其中第一项是复选框,用于从下面的下拉列表中选择所有条目。使用硬编码的字符串可以正常工作,但现在我想对其进行本地化......想从资源文件中设置字符串,但由于它的深度插入无法添加它。为此,我尝试添加面板和一个复选框以及文本块,也只是将文本块与复选框分开。

这是代码:

        <ComboBox Name="CmbEntries" IsTabStop="{Binding CurrentDialogViewModel, Converter={StaticResource NullToBoolConverter}}" Grid.Column="1" Grid.Row="4" VerticalAlignment="Top" Margin="2,0,0,5" SelectedItem="{Binding SelectedEntries}" SelectedIndex="0" HorizontalAlignment="Left" Width="400">
            <ComboBox.Resources>
                <CollectionViewSource x:Key="EntriesCollection" Source="{Binding DicomDir.Entries}"/>
            </ComboBox.Resources>
            <ComboBox.ItemsSource>
                <CompositeCollection>
                    <ComboBoxItem>
                        <CheckBox x:Name="all" Margin="8,0,0,0" 
                                  IsChecked="{Binding IsAllEntriessSelected, Mode=TwoWay}"
                                  Command="{Binding SelectAllEntriessCommand}"
                                  CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
                                  **Content="Select All Entries"** />
                    </ComboBoxItem>
                    <CollectionContainer Collection="{Binding Source={StaticResource EntriesCollection}}"/>
                </CompositeCollection>
            </ComboBox.ItemsSource>
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox Margin="8,0,0,0" 
                                  IsChecked="{ Binding Path=IsAnyEntitySelected, Mode=TwoWay }"
                                  Command="{Binding SelectAllEntityCommand}"
                                  CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}" />
                        <TextBlock Margin="5,0,0,0" TextWrapping="Wrap" Text="{Binding DisplayEntriesInfo}"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
            <ComboBox.ItemContainerStyle>
                <Style TargetType="ComboBoxItem">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding HasSelectedEntities}" Value="True">
                            <Setter Property="FontWeight" Value="Bold"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>

Scrshot1-Expected Scrshot2-Actual with Resx

这种情况下,可以直接从资源文件中读取吗?

【问题讨论】:

  • 在此而不是 Content="Select All Entries" 我想使用 **Text="{LocalizeExtension:Resx SelectAllEntries_Label}" ** 之类的东西。跨度>

标签: wpf checkbox


【解决方案1】:

要将内容绑定到您所做的资源:

<Window ...
        xmlns:properties="clr-namespace:YourNamespace.Properties">
    <Grid>
        <CheckBox Content="{x:Static properties:Resources.Foo}" />
    </Grid>
</Window>

为此,您需要公开资源。 如果不是,你会得到:

Provide value on 'System.Windows.Markup.StaticExtension' threw an exception...
     StaticExtension value cannot be resolved to an enumeration, static field, or static property.

【讨论】:

  • 约翰,请检查我添加的屏幕截图。在这个文件中,我已经将 resx 用于其他文本块元素。如果直接在具有属性 Content="{LocalizeExtension:ResxExtension Shell_SelectAllEntries}" 的同一网格中插入复选框,而不是组合框,则其工作正常。但是当我在组合框中使用复选框时,如屏幕截图所示,那时它显示了一些奇怪的文本[ref scrShot2]。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
相关资源
最近更新 更多