【问题标题】:How to get the control inside datatemplate of a combobox in silverlight4如何在silverlight4中获取组合框的数据模板内部的控件
【发布时间】:2013-01-11 17:22:11
【问题描述】:

我在silverlight4 的组合框的数据模板中有一个复选框。现在我想在它的 selectedindex 更改事件上获取组合框中的复选框。那么我该怎么做呢?

这是我的代码:

 <ComboBox Height="Auto" x:Name="CB_Categories" SelectionChanged="CB_Categories_SelectionChanged" Tag="">
                                <ComboBox.ItemTemplate>
                                    <DataTemplate>
                                    <CheckBox IsChecked="False" Content="{Binding Name}" CommandParameter="{Binding ProductCategoryID}" Click="CheckBox_Click" />
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>

请帮帮我。

谢谢,

【问题讨论】:

    标签: silverlight silverlight-4.0 silverlight-3.0 silverlight-5.0


    【解决方案1】:

    虽然可以这样做,但最好将要调查或更改的复选框属性绑定到背景视图模型或控制器中的值。

    例如如果您想将 IsChecked 更改为 true,请尝试以下操作:

    <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Description}" />
    

    然后,您可以使用 ItemsSource 属性将 ComboBox 连接到项目列表:

    <ComboBox ItemsSource="{Binding Options}">
        <ComboBox.ItemTemplate>
            <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Description}" />
        </ComboBox.ItemTemplate>
    </ComboBox>
    

    您还必须设置容器控件的DataContextComboBox,以便控件可以访问具有这些属性的对象。

    这样做意味着您可以在控件或窗口的代码隐藏中使用更少的代码,这些代码只是在执行诸如更新控件之类的管道工作。

    如果您需要更多示例,请告诉我...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-03
      • 2012-01-23
      • 1970-01-01
      • 2018-08-05
      • 2014-08-15
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多