【问题标题】:How can I bind radiobutton's checked property inside DataTemplate?如何在 DataTemplate 中绑定单选按钮的选中属性?
【发布时间】:2011-08-21 00:17:30
【问题描述】:

这是我的数据模板:

<DataTemplate x:Key="DataTemplateThemes">
            <Grid Width="160" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="5,0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="28.3336664835612"/>
                    <RowDefinition Height="161"/>
                    <RowDefinition Height="Auto" />
                    <RowDefinition/>

                </Grid.RowDefinitions>
                <TextBlock x:Name="tbThemeName" HorizontalAlignment="Left" Margin="2" Grid.Row="2" TextWrapping="Wrap" 
                           Text="{Binding ThemeName}" FontSize="16" FontFamily="Segoe UI" d:LayoutOverrides="Height"/>
                <Image Margin="0" Stretch="Fill" Grid.Row="1" Source="{Binding Path=ThemePicName, Converter={StaticResource imageConverter}}"/>
                <TextBlock x:Name="tbDescription" HorizontalAlignment="Left" Margin="2" Grid.Row="3" 
                           TextWrapping="Wrap" Text="{Binding ThemeDescription}" FontSize="14.667" FontFamily="Segoe UI" 
                           d:LayoutOverrides="Height"/>
                <RadioButton Content="Apply" Margin="-10,0,0,0" 
                             d:LayoutOverrides="Width, Height" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
        </DataTemplate>

RadioButton 的 IsChecked 属性取决于我的 ViewModel 中可用的 Theme 属性。最初我想我会创建一个转换器,它将传递当前行(datatemplate current DataContext)的 ThemeId 和 ViewModel 的 Theme(这是选定的 Theme)的 ThemeId 属性,如果它们都匹配,我会检查单选按钮。但是 ConverterParameter 不能采用 Binding 表达式,因为它不是 DependencyProperty。那么我该如何解决这个问题呢?

提前致谢:)

【问题讨论】:

  • 因为您可以获得主题ID和选定的主题ID。为什么不在您的视图模型中创建一个布尔属性并从那里检查它们是否匹配?
  • 如果 ThemeId 和 Theme 属性在同一个视图模型中 - 传递整个视图模型。如果它更复杂 - 我会使用绑定到 ListBox 的 IsSelected 属性,更容易绑定到 SelectedItem 属性。
  • @Xin:- 不知道为什么我没有想到。

标签: silverlight silverlight-4.0


【解决方案1】:

您可以实施多重绑定解决方案。我之前已经实现了这里描述的解决方案:

http://www.scottlogic.co.uk/blog/colin/2010/05/silverlight-multibinding-solution-for-silverlight-4/

然后你可以有一个接受两个必需参数的转换器。

【讨论】:

    猜你喜欢
    • 2016-01-08
    • 1970-01-01
    • 2012-12-27
    • 2016-04-24
    • 1970-01-01
    • 2020-05-22
    • 2012-03-02
    • 2011-04-27
    相关资源
    最近更新 更多