你好,我是WPF的初学者,我想做一个类似淘宝星级评价的一串可选五角星,我该怎么做,请给我详细指导,急!
展开全部
写了个demo给你。
前台:
<Grid Name="grid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" FontSize="14">请打分:</TextBlock>
<ListBox Height="42" Name="listBox1" Width="260" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Width" Value="50"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
</ListBox.Resources>
<ListBoxItem/>
<ListBoxItem/>
<ListBoxItem/>
<ListBoxItem/>
<ListBoxItem/>
</ListBox>
</StackPanel>
<TextBlock x:Name="txtResult" Grid.Row="1" Height="25" Width="50" HorizontalAlignment="Left"/>
</Grid>
后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string NumofStars = "Null";
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox lb = sender as ListBox;
if (lb.SelectedIndex < 0)
return;
for (int i = 0; i < lb.Items.Count; i++)
{
ListBoxItem lbitem = lb.Items[i] as ListBoxItem;
if (i <= lb.SelectedIndex)
{
lbitem.Background = new SolidColorBrush(Colors.Yellow);
}
else
{
lbitem.Background = new SolidColorBrush(Colors.White);
}
}
NumofStars = (lb.SelectedIndex + 1).ToString();
txtResult.Text = NumofStars;
}
}
左下角的textblock用来显示所打的分数。我只做了功能哈。忽略了控件模板和样式。像星星形状的话就要编辑listbox的ItemTemplate即可。细节样式丢给style去处理。
前台:
<Grid Name="grid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" FontSize="14">请打分:</TextBlock>
<ListBox Height="42" Name="listBox1" Width="260" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Width" Value="50"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
</ListBox.Resources>
<ListBoxItem/>
<ListBoxItem/>
<ListBoxItem/>
<ListBoxItem/>
<ListBoxItem/>
</ListBox>
</StackPanel>
<TextBlock x:Name="txtResult" Grid.Row="1" Height="25" Width="50" HorizontalAlignment="Left"/>
</Grid>
后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string NumofStars = "Null";
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox lb = sender as ListBox;
if (lb.SelectedIndex < 0)
return;
for (int i = 0; i < lb.Items.Count; i++)
{
ListBoxItem lbitem = lb.Items[i] as ListBoxItem;
if (i <= lb.SelectedIndex)
{
lbitem.Background = new SolidColorBrush(Colors.Yellow);
}
else
{
lbitem.Background = new SolidColorBrush(Colors.White);
}
}
NumofStars = (lb.SelectedIndex + 1).ToString();
txtResult.Text = NumofStars;
}
}
左下角的textblock用来显示所打的分数。我只做了功能哈。忽略了控件模板和样式。像星星形状的话就要编辑listbox的ItemTemplate即可。细节样式丢给style去处理。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询