我的ProgressBarXAML:
<Window ...>
<ProgressBar Value="{Binding Path=Progress}"/>
</Window>
代碼隱藏:
internal int Progress { get; set; }
此進度變數是 1 的倍數,因此值為 1、2、3,依此類推...
現在我決定對's使用相同的Progress屬性。但是,我發現它只接受 0.0 到 1.0 的范圍(即標準化值)。有沒有一種方法可以系結相同的屬性,同時指定某種轉換,例如除以數字以使其標準化?TaskBarItemInfoProgressValueProgress
uj5u.com熱心網友回復:
有多種選擇。您可以公開一個單獨的(可能是計算的)double屬性,該屬性在每次Progress更改屬性時都會更新,例如,通過在計算屬性的設定器中引發PropertyChanged( INotifyPropertyChanged) 事件。
另一種選擇是創建自定義值或多值轉換器。如果您的int進度值有硬編碼或預定義的范圍,您可以創建一個像這樣的值轉換器。
public class AbsoluteToRelativeProgressConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is int progress) ||
!(parameter is string maximumText) ||
!int.TryParse(maximumText, out var maximum))
return Binding.DoNothing;
if (maximum < progress)
throw new ArgumentException("Progress must not exceed maximum.");
return progress / (double)maximum;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return new NotImplementedException();
}
}
此轉換器獲取作為value. 如果它真的是一個int. 作為引數,您可以傳遞最大值。它被傳遞為string簡化 XAML 中的語法(分配 anint是冗長的)。轉換器回傳范圍 [0.0, 1.0] 中的相對進度double。如前所述,如果最大值是硬編碼的,您可以洗掉引數用法并將 替換maximum為值。
Window.Resources接下來,在范圍內的或任何其他資源字典中創建轉換器的實體。
<Window.Resources>
<local:AbsoluteToRelativeProgressConverter x:Key="AbsoluteToRelativeProgressConverter"/>
</Window.Resources>
然后將Progressin系結TaskBarItemInfo到ProgressValue使用轉換器。ConverterParameter用于傳遞最大值,這里作為100示例。請注意,您需要將 設定ProgressState為 eg或Normal,否則將不會顯示進度條。ErrorPaused
<Window.TaskbarItemInfo>
<TaskbarItemInfo ProgressState="Normal"
ProgressValue="{Binding Progress, Converter={StaticResource AbsoluteToRelativeProgressConverter}, ConverterParameter=100}"/>
</Window.TaskbarItemInfo>
<Window.Resources>
<local:AbsoluteToRelativeProgressMultiConverter x:Key="AbsoluteToRelativeProgressMultiConverter"/>
</Window.Resources>
如果你也想系結最大值,你需要創建一個IMultiValueConverter這樣的。
public class AbsoluteToRelativeProgressMultiConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values is null ||
values.Length != 2 ||
!(values[0] is int progress) ||
!(values[1] is int maximum))
return Binding.DoNothing;
if (maximum < progress)
throw new ArgumentException("Progress must not exceed maximum.");
return progress / (double)maximum;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
它的作業原理相同,但允許系結多個值。在這里,進度是轉換器的第一個值,最大值是第二個。您也可以輕松擴展此轉換器以系結最小值。
在 XAML 中,您也可以創建一個實體,但這次您使用的是MultiBinding.
<Window.Resources>
<local:AbsoluteToRelativeProgressMultiConverter x:Key="AbsoluteToRelativeProgressMultiConverter"/>
</Window.Resources>
<Window.TaskbarItemInfo>
<TaskbarItemInfo ProgressState="Normal">
<TaskbarItemInfo.ProgressValue>
<MultiBinding Converter="{StaticResource AbsoluteToRelativeProgressMultiConverter}">
<Binding Path="Progress"/>
<Binding Path="Maximum"/>
</MultiBinding>
</TaskbarItemInfo.ProgressValue>
</TaskbarItemInfo>
</Window.TaskbarItemInfo>
請注意,如果您想ProgressState依賴于Progress屬性,例如設定Normal進度是否為>= 0and <= maximum,猜猜看,您可以使用創建轉換器......當然也可以使用單獨的屬性。
uj5u.com熱心網友回復:
xml代碼:
<Window.Resources>
<local:DataConverter x:Key="dataConverter " />
</Window.Resources>
<ProgressBar Value={Binding Path=Progress, Converter={StaticResource dataConverter }}/>
轉換器代碼
public class DataConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// read the input from value and cast it
// return your object;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424242.html
