如何在visualstate中绑定动态值

时间:2015-07-23 06:11:24

标签: c# wpf xaml windows-store-apps winrt-xaml

在通用Windows应用程序中如何在XAML中绑定视觉状态内的值。为了便于理解,我删除了除下面代码中的VisualStateGroups下的checked状态以外的其他状态。

  <Page.Resources>
   <converters:UriToImageBrushConverter x:Key="cnvt"/>
    <Style x:Key="RadioButtonStyletest" TargetType="RadioButton">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Foreground" Value="{ThemeResource RadioButtonContentForegroundThemeBrush}"/>
        <Setter Property="Padding" Value="1,4,0,0"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
        <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="RadioButton">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                        <VisualStateManager.VisualStateGroups>                               
                            <VisualStateGroup x:Name="CheckStates">
                                <VisualState x:Name="Checked">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="CheckGlyph"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unchecked"/>
                                <VisualState x:Name="Indeterminate"/>
                            </VisualStateGroup>                             
                        </VisualStateManager.VisualStateGroups>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="29"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid VerticalAlignment="Top">
                                <Ellipse x:Name="BackgroundEllipse" Height="23" Fill="{Binding Imgsrc, Converter={StaticResource cnvt}}" Stroke="{ThemeResource RadioButtonBorderThemeBrush}" StrokeThickness="{ThemeResource RadioButtonBorderThemeThickness}" UseLayoutRounding="False" Width="23">

                                </Ellipse>
                                <Ellipse x:Name="CheckGlyph" Fill="{ThemeResource RadioButtonForegroundThemeBrush}" Height="13" Opacity="0" UseLayoutRounding="False" Width="13"/>
                                <Rectangle x:Name="FocusVisualWhite" Height="29" Opacity="0" StrokeDashOffset="1.5" StrokeEndLineCap="Square" Stroke="{ThemeResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1" Width="29"/>
                                <Rectangle x:Name="FocusVisualBlack" Height="29" Opacity="0" StrokeDashOffset="0.5" StrokeEndLineCap="Square" Stroke="{ThemeResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1" Width="29"/>
                            </Grid>
                            <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" Grid.Column="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

在上面的代码中,我试图绑定BackgroundEllipse背景,但它没有发生。

c#c​​ode:

private String imgsrc;

    public String Imgsrc
    {
        get
        {
            return imgsrc;
        }
        set
        {
            imgsrc = value;
        }
    }

    public MainPage()
    {
        this.InitializeComponent();

        Imgsrc = "/Assets/yes.png";

    }

转换器

class UriToImageBrushConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        try
        {
            return new BitmapImage(new Uri((string)value));
        }
        catch
        {
            return new BitmapImage();
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

}

请告诉我哪里出错了。

1 个答案:

答案 0 :(得分:2)

默认情况下,xaml中的绑定在DataContext中查找属性。但是,ImageBrush不是FrameworkElement,也没有DataContext。

试试这个:

<Ellipse x:Name="BackgroundEllipse"
         Fill="{Binding Imgsrc, Converter={StaticResource UriToImageBrushConverter}}" />

UriToImageBrushConverter是一个实现IValueConverter的简单类,我确定你不需要我编写它。

另外请确保BackgroundEllipse元素的DataContext是MainPage。否则你可以使用RelativeSource:

<Ellipse x:Name="BackgroundEllipse"
         Fill="{Binding Imgsrc, RelativeSource={RelativeSource AncestorType=Page}}
                        Converter={StaticResource UriToImageSourceConverter}}" />