C# 如何在visualstate中绑定动态值

C# 如何在visualstate中绑定动态值,c#,wpf,xaml,windows-store-apps,winrt-xaml,C#,Wpf,Xaml,Windows Store Apps,Winrt Xaml,在通用Windows应用程序中,如何在XAML的可视状态中绑定值。为了便于理解,我删除了下面代码中VisualStateGroups下除checked state之外的其他状态 <Page.Resources> <converters:UriToImageBrushConverter x:Key="cnvt"/> <Style x:Key="RadioButtonStyletest" TargetType="RadioButton">

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

  <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>
转换器

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();
    }
}
}


请告诉我哪里出错了。

默认情况下,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}}" />


上面的代码不清楚radiobutton上有什么datacontext。调试时还要检查输出窗口以查看任何绑定错误Shi Liero,为了便于理解,我只提到了CheckState,并避免了其他VisualStateGroup(CommonState、Focustates)。我没有收到任何绑定错误。这是将字符串(Imgsrc)绑定到ImageSource的正确方法吗。为什么ImageBrush.ImageSource上有双向绑定?实际上不需要双向绑定,我以这种方式进行了测试。我也按照您的建议进行了尝试。它不起作用。我已经更新了上面的代码。我们能在页面的资源中绑定一个值吗?你们两个都试过了吗?您是否尝试将断点设置为UriToImageSourceConverter.Convert方法以检查它是否实际被调用?你设置了数据上下文吗?除非将pafe指定为datacontext,否则绑定不会绑定到页面上定义的属性。它必须起作用。问题隐藏在您尚未共享的细节中。
<Ellipse x:Name="BackgroundEllipse"
         Fill="{Binding Imgsrc, RelativeSource={RelativeSource AncestorType=Page}}
                        Converter={StaticResource UriToImageSourceConverter}}" />