如何在WinUI 3中清除RatingControl并将其设置为0颗星?

huangapple go评论86阅读模式
英文:

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:

&lt;StackPanel Orientation=&quot;Horizontal&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;4&quot;&gt;
    &lt;RatingControl x:Name=&quot;mainRating&quot; Margin=&quot;5, 10, 10, 0&quot; HorizontalAlignment=&quot;Left&quot; VerticalAlignment=&quot;Center&quot; VerticalContentAlignment=&quot;Center&quot; AutomationProperties.Name=&quot;Simple RatingControl&quot; IsClearEnabled=&quot;True&quot; IsReadOnly=&quot;False&quot; ValueChanged=&quot;MainRating_ValueChanged&quot; /&gt;
    &lt;Button x:Name=&quot;clearRatingButton&quot; Content=&quot;Clear Rating&quot; Click=&quot;ClearRatingButton_Click&quot; Visibility=&quot;Collapsed&quot; /&gt;
&lt;/StackPanel&gt;

And here is my code for when the &quot;Clear Rating&quot; 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:

如何在WinUI 3中清除RatingControl并将其设置为0颗星?

Here is my XAML:

&lt;StackPanel Orientation=&quot;Horizontal&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;4&quot;&gt;
&lt;RatingControl x:Name=&quot;mainRating&quot; Margin=&quot;5, 10, 10, 0&quot; HorizontalAlignment=&quot;Left&quot; VerticalAlignment=&quot;Center&quot; VerticalContentAlignment=&quot;Center&quot; AutomationProperties.Name=&quot;Simple RatingControl&quot; IsClearEnabled=&quot;True&quot; IsReadOnly=&quot;False&quot; ValueChanged=&quot;MainRating_ValueChanged&quot; /&gt;
&lt;Button x:Name=&quot;clearRatingButton&quot; Content=&quot;Clear Rating&quot; Click=&quot;ClearRatingButton_Click&quot; Visibility=&quot;Collapsed&quot; /&gt;
&lt;/StackPanel&gt;

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?

如何在WinUI 3中清除RatingControl并将其设置为0颗星?

答案1

得分: 2

所有依赖属性都有与之关联的元数据。公共实例的属性获取/设置方法仅是依赖属性GetValueSetValue方法的包装器

您还可以使用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);

huangapple
  • 本文由 发表于 2023年3月7日 13:35:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定