以下类型
public class Protocol
{
public int Id {get;set}
public string Name {get;set}
public int Speed {get;set}
public int Security {get;set}
}
所以我制作了一个自定义评级控件,它采用图像和整数(即速度或安全性)来填充带有图像的矩形WrapPanel。 我为组合框创建了DataTemplate,如下所示
<ComboBox x:Name="pro" IsSynchronizedWithCurrentItem="True" Grid.Row="1" Margin="40,41,50,0" VerticalAlignment="Top"
Style="{StaticResource ProtocolComboBoxStyle}"
ItemsSource="{Binding Protocols}"
SelectedItem="{Binding Protocol, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Height="29" SelectionChanged="ComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type models:Protocol}">
<WrapPanel Orientation="Horizontal">
<TextBlock Width="145" Padding="10,2,0,2" x:Name="text" Style="{StaticResource SettingsInactiveTextBlockStyle}" Text="{Binding Name}" HorizontalAlignment="Center"/>
<controls:RatingControl FilledImage="pack://application:,,,/SampleApp;component/Resources/Images/Settings/SettingsHome/security_filled.png"
UnfilledImage="pack://application:,,,/SampleApp;component/Resources/Images/Settings/SettingsHome/security_unfilled.png"
Rating="{Binding Security}" HorizontalAlignment="Right"/>
<controls:RatingControl Margin="5,2,0,0" FilledImage="pack://application:,,,/SampleApp;component/Resources/Images/Settings/SettingsHome/speed_filled.png"
UnfilledImage="pack://application:,,,/SampleApp;component/Resources/Images/Settings/SettingsHome/speed_unfilled.png"
Rating="{Binding Speed}" HorizontalAlignment="Right"/>
</WrapPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
除了初始选择之外,它工作正常,只有协议名称在togglebutton中发生变化,左边的评级控件不会改变。
另外,选择项目确实会更改视图模型中的绑定selectedProtocol属性。
请告诉我,如果我遗失了什么? 提前致谢