我创建了一个带有dataTemplate的ItemControl,其中包含一个将根据ItemsSource进行着色的矩形。提交给我的应用程序的日期是一个颜色十六进制代码,不包含井号(#)。只需一个6个字符的字符串。为了使颜色正确显示,我需要使用前面的#来格式化6个字符的字符串。 exp#A31F34
这是XAML
<DataTemplate x:Key="ColorSequenceSwatchPreviews">
<Rectangle Name="ColorSwatch" Height="20" Width="120" RadiusX="3" RadiusY="3" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0,3,0,3">
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Fill">
<Setter.Value>
<MultiBinding>
<MultiBinding.StringFormat><![CDATA[#{0}]]></MultiBinding.StringFormat>
<Binding Path="InnerXml" Mode="OneWay" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Rectangle.Style>
</Rectangle>
我正在使用MultiBinding.StringFormat将字符串格式化为正确的Hexcode,但是我很难理解为什么矩形的填充没有着色。
如果我使用TextBox进行MultiBinding,我可以让矩形颜色化,然后将矩形的fill属性绑定到textBox的Text属性。但是,我更喜欢直接从矩形的填充属性中绑定,就像在我的第一个例子中一样,因为它更干净。
<DataTemplate x:Key="ColorSequenceSwatchPreviews">
<StackPanel Orientation="Horizontal" Margin="0,3,0,3" VerticalAlignment="Center" HorizontalAlignment="Left">
<TextBox x:Name="Hexcode" Visibility="Collapsed">
<TextBox.Text>
<MultiBinding>
<MultiBinding.StringFormat><![CDATA[#{0}]]></MultiBinding.StringFormat>
<Binding Path="InnerXml" Mode="OneWay" />
</MultiBinding>
</TextBox.Text>
</TextBox>
<Rectangle Name="ColorSwatch" Height="20" Width="120" RadiusX="3" RadiusY="3" VerticalAlignment="Center" HorizontalAlignment="Left">
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Fill" Value="{Binding ElementName=Hexcode,Path=,Mode=OneWay}" />
</Style>
</Rectangle.Style>
</Rectangle>
</StackPanel>
有没有办法让第一个例子起作用,或者我是否仍然坚持使用第二个例子中的代码?
答案 0 :(得分:1)
使用转换器可以更轻松地实现这一点。你根本不需要submit
。简单的<!-- Notice the getScore() function will be called when the form is submitted -->
<form id="NPSform" ... onsubmit='getScore();'>
<!-- Elements omitted for brevity -->
<input type="submit" name="mysubmit" value="Submit"/>
</form>
<script>
function getScore(){
// Get the selected score (assuming one was selected)
var score = document.querySelector('input[name="scores"]:checked').value;
alert(score + ' was selected!');
}
</script>
和MultiBinding
应该这样做:
这是转换器:
Binding
您现在需要做的就是在参考资料部分创建转换器的对象:
Converter
(local是您定义此转换器类的项目的命名空间)
然后在<ValueConversion(GetType(String), GetType(SolidColorBrush))>
Public Class HexToBrushConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
Return DirectCast(New BrushConverter().ConvertFrom("#" & value.ToString()), SolidColorBrush)
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Return Nothing
End Function
End Class
属性中使用它:
<local:HexToBrushConverter x:Key="HexToBrushConverter" />