This commit is contained in:
NaBian committed 2019-08-18 14:37:38 +08:00
1 parent 16b155f579
commit 14fff8cf16
2 files changed
+39 -15

No files matched your search

@@ -44,6 +44,8 @@ namespace HandyControl.Controls
private bool _isLoaded; private bool _isLoaded;
private bool _updateItems;
public Rate() public Rate()
{ {
AddHandler(RateItem.SelectedChangedEvent, new RoutedEventHandler(RateItemSelectedChanged)); AddHandler(RateItem.SelectedChangedEvent, new RoutedEventHandler(RateItemSelectedChanged));
@@ -52,8 +54,15 @@ namespace HandyControl.Controls
Loaded += (s, e) => Loaded += (s, e) =>
{ {
if (DesignerHelper.IsInDesignMode) return; if (DesignerHelper.IsInDesignMode) return;
_updateItems = false;
OnApplyTemplateInternal();
_updateItems = true;
UpdateItems();
if (_isLoaded) return; if (_isLoaded) return;
_isLoaded = true; _isLoaded = true;
if (Value <= 0) if (Value <= 0)
{ {
if (DefaultValue > 0) if (DefaultValue > 0)
@@ -164,19 +173,28 @@ namespace HandyControl.Controls
return new RateItem(); return new RateItem();
} }
private void OnApplyTemplateInternal()
{
Items.Clear();
for (var i = 1; i <= Count; i++)
{
var item = new RateItem
{
Index = i
};
Items.Add(item);
}
}
public override void OnApplyTemplate() public override void OnApplyTemplate()
{ {
if (!_isLoaded) if (!_isLoaded)
{ {
Items.Clear(); _updateItems = true;
OnApplyTemplateInternal();
for (var i = 1; i <= Count; i++) _updateItems = false;
{
Items.Add(new RateItem
{
Index = i
});
}
} }
base.OnApplyTemplate(); base.OnApplyTemplate();
@@ -191,7 +209,7 @@ namespace HandyControl.Controls
protected override void UpdateItems() protected override void UpdateItems()
{ {
if (!_isLoaded) return; if (!_isLoaded || !_updateItems) return;
var count = (int) Value; var count = (int) Value;
for (var i = 0; i < count; i++) for (var i = 0; i < count; i++)
@@ -46,7 +46,13 @@ namespace HandyControl.Controls
public RateItem() public RateItem()
{ {
Loaded += (s, e) => _isLoaded = true; Loaded += (s, e) =>
{
if (_isLoaded) return;
_isLoaded = true;
OnApplyTemplate();
};
} }
public bool AllowClear public bool AllowClear
@@ -87,7 +93,7 @@ namespace HandyControl.Controls
if (_isHalf == value) return; if (_isHalf == value) return;
_isHalf = value; _isHalf = value;
if (_icon == null) return; if (_icon == null) return;
_icon.Width = value ? ActualWidth / 2 : ActualWidth; _icon.Width = value ? Width / 2 : Width;
} }
} }
@@ -134,7 +140,7 @@ namespace HandyControl.Controls
if (IsReadOnly) return; if (IsReadOnly) return;
if (!AllowHalf) return; if (!AllowHalf) return;
var p = e.GetPosition(this); var p = e.GetPosition(this);
IsHalf = p.X < ActualWidth / 2; IsHalf = p.X < Width / 2;
} }
public override void OnApplyTemplate() public override void OnApplyTemplate()
@@ -147,7 +153,7 @@ namespace HandyControl.Controls
{ {
if (_icon == null) return; if (_icon == null) return;
_icon.Show(IsSelected); _icon.Show(IsSelected);
_icon.Width = IsHalf ? ActualWidth / 2 : ActualWidth; _icon.Width = IsHalf ? Width / 2 : Width;
} }
} }
@@ -202,7 +208,7 @@ namespace HandyControl.Controls
if (AllowHalf) if (AllowHalf)
{ {
var p = e.GetPosition(this); var p = e.GetPosition(this);
IsHalf = p.X < ActualWidth / 2; IsHalf = p.X < Width / 2;
} }
} }
} }