自定义Toggleswitch绑定到对象

时间:2011-03-13 16:55:02

标签: windows-phone-7 silverlight-toolkit

我正在尝试创建一个toggleswitch,它将在datatemplate中有多行绑定到单个对象的不同属性。这些切换开关将位于列表框内。

下面的我的xaml代码显示了当前的toggleswitch模板。使用下面的代码,只有标题正确绑定。我需要另外两行(在ContentTemplate中)和toggleswitch本身绑定到对象的布尔属性。

                        <DataTemplate>
                            <toolkit:ToggleSwitch Header="{Binding Property1}" Width="450">
                                <toolkit:ToggleSwitch.HeaderTemplate>
                                    <DataTemplate>
                                        <ContentControl FontWeight="Black" FontSize="40" Foreground="{StaticResource PhoneForegroundBrush}" Content="{Binding}" VerticalAlignment="Top" />
                                    </DataTemplate>
                                </toolkit:ToggleSwitch.HeaderTemplate>
                                <toolkit:ToggleSwitch.ContentTemplate>
                                    <DataTemplate>
                                        <StackPanel>
                                            <TextBlock Text="{Binding Property2}" FontSize="32" FontWeight="Light"  Foreground="{StaticResource PhoneAccentBrush}" />
                                            <TextBlock Text="{Binding Property3}" FontSize="{StaticResource PhoneFontSizeSmall}" Foreground="{StaticResource PhoneSubtleBrush}" />
                                        </StackPanel>
                                    </DataTemplate>
                                </toolkit:ToggleSwitch.ContentTemplate>
                            </toolkit:ToggleSwitch>
                        </DataTemplate>                            

有关如何实现我需要的结果的任何建议吗?

提前致谢!

这是我想要实现的目标

Property1 
Property2:On/Off                  [===]    (this is the toggle switch)
Property3

属性1,2和3也都有自定义格式。请记住,这些将在列表框中,因此它们将绑定到集合。

2 个答案:

答案 0 :(得分:1)

您需要修改转换器中ToggleSwitch的来源。

添加额外的文本/字符串属性以将Property2和Property3绑定到。 (这些也需要是不同模板的单独项目 - 就像报警应用程序一样。)

然后看看更改ContentProperty的绑定或扩展OffOnConverter以包含其他新的属性。

答案 1 :(得分:0)

可能你需要的是绑定到一个元素,请查看这篇文章:

Silverlight 3 Element binding in a datatemplate

为元素提供数据模板x:Name属性,然后在数据模板中使用元素绑定。

希望这有帮助, 罗伯特