我有一个矩形,我希望根据我的page.cs中的值是否在某些值之间设置4个不同的图标,例如:
If value is 0 show icon 1.
if value is larger than 0 but smaller or equal to 0.25 show icon 2.
If value is >0.25 && value is <=0.5 show icon 3.
If value is >0.5 && value is <=0.75, show icon 4.
and If value is >0.75 && value is <=1 show icon 5.
这是我的变量的样子:
private double _double = 0.25;
public double Volume
{
get
{
return _double;
}
set
{
_double = value;
OnPropertyChanged("Volume");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
我还在INotifyPropertyChanged
partial class
以下是我使用的转换器:
[ValueConversion(typeof(double), typeof(int))]
public class VolumePicture : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (System.Convert.ToDouble(value) <= 0.25)
{
return 1;
}
else if (System.Convert.ToDouble(value) > 0.25 && System.Convert.ToDouble(value) <= 0.5)
{
return 2;
}
else if (System.Convert.ToDouble(value) > 0.5 && System.Convert.ToDouble(value) <= 0.75)
{
return 3;
}
else if (System.Convert.ToDouble(value) > 0.75 && System.Convert.ToDouble(value) <= 1)
{
return 4;
}
else
{
return 0;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
它基本上需要输入,并且完全按照我在第一部分中的描述进行操作。
Aaaand这里是我对矩形的XAML,我想更改opacitymask:
<Rectangle Height="20" Margin="0,2,0,0">
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Fill" Value="#FF929292" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, ElementName=VolumeButton}"
Value="True">
<Setter Property="Fill" Value="White" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=NowPlaying.Volume, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource VolumePictureConverter}}" Value="5">
<Setter Property="OpacityMask">
<Setter.Value>
<VisualBrush Visual="{StaticResource appbar_sound_mute}"/>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=NowPlaying.Volume, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ResourceKey=VolumePictureConverter}}" Value="1">
<Setter Property="OpacityMask">
<Setter.Value>
<VisualBrush Visual="{StaticResource appbar_sound_0}"/>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=NowPlaying.Volume, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ResourceKey=VolumePictureConverter}}" Value="2">
<Setter Property="OpacityMask">
<Setter.Value>
<VisualBrush Visual="{StaticResource appbar_sound_1}"/>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=NowPlaying.Volume, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ResourceKey=VolumePictureConverter}}" Value="3">
<Setter Property="OpacityMask">
<Setter.Value>
<VisualBrush Visual="{StaticResource appbar_sound_2}"/>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=NowPlaying.Volume, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ResourceKey=VolumePictureConverter}}" Value="4">
<Setter Property="OpacityMask">
<Setter.Value>
<VisualBrush Visual="{StaticResource appbar_sound_3}"/>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
这可能看起来很混乱,为此,我很抱歉。
但是,这就是我的问题:当我在运行时更改Volume变量时,似乎什么也没发生。我认为转换器没有触发,但我不知道为什么会发生这种情况(!) 而且由于我在绑定和转换器以及WPF方面确实不太好,所以我已经陷入了困境......
任何人都知道麻烦制造者会是什么?
谢谢
有一点需要注意的是,我并不总是拥有&#34; NowPlaying.Volume&#34;在那里的路径,我只是尝试了一堆东西。 当我进行调试时,我会在输出中将其重命名(将其重命名为&#34;路径错误:&#39;音量&#39;&#34;当我删除Nowplaying时。
System.Windows.Data Error: 40 : BindingExpression path error: 'NowPlaying' property not found on 'object' ''Rectangle' (Name='')'. BindingExpression:Path=NowPlaying.Volume; DataItem='Rectangle' (Name=''); target element is 'Rectangle' (Name=''); target property is 'NoTarget' (type 'Object')
答案 0 :(得分:1)
似乎您绑定到Rectangle
而不是它的DataContext。 Rectangle
没有属性Volume
,因此您将收到绑定错误。尝试将绑定语句更改为类似的内容(请注意此部分:...Path=DataContext.Volume...
):
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
.......
<Style.Triggers>
.......
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=DataContext.Volume, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource VolumePictureConverter}}"
Value="5">
<Setter Property="OpacityMask">
<Setter.Value>
<VisualBrush Visual="{StaticResource appbar_sound_mute}"/>
</Setter.Value>
</Setter>
</DataTrigger>
.......
</Style.Triggers>
.......
</Style>
</Rectangle.Style>