英文:
How do I clear a RatingControl and make it 0 stars in WinUI 3?
问题
以下是代码部分的翻译:
<My UI:
[![enter image description here][1]][1]
Here is my XAML:
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="4">
<RatingControl x:Name="mainRating" Margin="5, 10, 10, 0" HorizontalAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center" AutomationProperties.Name="Simple RatingControl" IsClearEnabled="True" IsReadOnly="False" ValueChanged="MainRating_ValueChanged" />
<Button x:Name="clearRatingButton" Content="Clear Rating" Click="ClearRatingButton_Click" Visibility="Collapsed" />
</StackPanel>
And here is my code for when the "Clear Rating" button is clicked:
private void ClearRatingButton_Click(object sender, RoutedEventArgs e)
{
mainRating.Value = 0.0;
clearRatingButton.Visibility = Visibility.Collapsed;
}
private void MainRating_ValueChanged(RatingControl sender, object args)
{
clearRatingButton.Visibility = Visibility.Visible;
}
But whenever that code is called, the stars on the control in the UI is always set to 1 star, not 0. Why?
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/4Cj7d.png
[2]: https://i.stack.imgur.com/0Ozth.png
英文:
My UI:
Here is my XAML:
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="4">
<RatingControl x:Name="mainRating" Margin="5, 10, 10, 0" HorizontalAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center" AutomationProperties.Name="Simple RatingControl" IsClearEnabled="True" IsReadOnly="False" ValueChanged="MainRating_ValueChanged" />
<Button x:Name="clearRatingButton" Content="Clear Rating" Click="ClearRatingButton_Click" Visibility="Collapsed" />
</StackPanel>
And here is my code for when the "Clear Rating" button is clicked:
private void ClearRatingButton_Click(object sender, RoutedEventArgs e)
{
mainRating.Value = 0.0;
clearRatingButton.Visibility = Visibility.Collapsed;
}
private void MainRating_ValueChanged(RatingControl sender, object args)
{
clearRatingButton.Visibility = Visibility.Visible;
}
But whenever that code is called, the stars on the control in the UI is always set to 1 star, not 0. Why?
答案1
得分: 2
所有依赖属性都有与之关联的元数据。公共实例的属性获取/设置方法仅是依赖属性GetValue
和SetValue
方法的包装器。
您还可以使用ClearValue方法将值重置为其默认值。
对于RatingControl,代码如下:
mainRating.ClearValue(RatingControl.ValueProperty);
请注意,这可以看作是以下快捷方式:
var dp = RatingControl.ValueProperty;
var def = dp.GetMetadata(typeof(RatingControl)).DefaultValue; // -1d
mainRating.SetValue(dp, def);
英文:
All dependency properties have associated metadata with it. The public instance's property get/set methods are merely wrappers over dependency properties GetValue
and SetValue
methods.
And you can also reset the value to it's default value using the ClearValue method.
In the case of RatingControl, that would be a code like this:
mainRating.ClearValue(RatingControl.ValueProperty);
Note this can be seen as a shortcut to:
var dp = RatingControl.ValueProperty;
var def = dp.GetMetadata(typeof(RatingControl)).DefaultValue; // -1d
mainRating.SetValue(dp, def);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论