0

我很小心地在论坛中检查了这一点,但我没有找到解决方案。

这很完美:

 <Image Margin="0,0,0,0" Stretch="UniformToFill" Source="http://localhost:5313/Images/1.png"    MaxHeight="140" MinHeight="155" VerticalAlignment="Top"/>

为什么这不起作用:

<Image Margin="0,0,0,0" Stretch="UniformToFill" Source="{Binding ImagenSeleccionada}"    MaxHeight="140" MinHeight="155" VerticalAlignment="Top"/>

后面的代码。

    private string _imagenSeleccionada;
    public string ImagenSeleccionada
    {
        get
        {
            return this._imagenSeleccionada;
        }
        set
        {
            if (this._imagenSeleccionada == value)
                return;

            this._imagenSeleccionada = value;
        }
    }

主页

控制

ItemsSource="{Binding FilteredTemperatureReports}" UseLayoutRounding="False" RowHeight="20" Horizo​​ntalAlignment="Stretch" VerticalAlignment="Stretch" SelectionMode="Single" Horizo​​ntalGridLinesBrush="Transparent" VerticalGridLinesBrush="Transparent" Background="{x:Null }" BorderBrush="{x:Null}" IsFilteringAllowed="False" RowIndicatorVisibility="Collapsed" CanUserFreezeColumns="False" ShowGroupPanel="False" HeaderRowStyle="{StaticResource GridViewHeaderRowStyle}" GroupRowStyle="{StaticResource GridViewGroupRowStyle}" RowStyle="{StaticResource GridViewRowStyle}">

    <telerik:GridViewDataColumn Width="150" DataMemberBinding="{Binding Medio}" IsReadOnly="True" Header="Medio" 
                                                HeaderCellStyle="{StaticResource GridViewHeaderCellStyleFirst}"
                                                CellStyle="{StaticResource GridViewCellStyle}" />
    <telerik:GridViewDataColumn Width="80" DataMemberBinding="{Binding TimeStamp}" IsReadOnly="True" Header="Fecha" DataFormatString="{} {0:dd/MM/yyyy}"  
                                                HeaderCellStyle="{StaticResource GridViewHeaderCellStyle}"
                                                CellStyle="{StaticResource GridViewCellStyle}"/>
    <telerik:GridViewDataColumn Width="200" DataMemberBinding="{Binding Producto}" IsReadOnly="True" Header="Producto" 
                                                HeaderCellStyle="{StaticResource GridViewHeaderCellStyle}"
                                                CellStyle="{StaticResource GridViewCellStyle}"/>
    <telerik:GridViewDataColumn Width="180" DataMemberBinding="{Binding Version}" IsReadOnly="True" Header="Version" 
                                                HeaderCellStyle="{StaticResource GridViewHeaderCellStyle}"
                                                CellStyle="{StaticResource GridViewCellStyle}"/>
    <telerik:GridViewDataColumn Width="80" DataMemberBinding="{Binding Inversion}" IsReadOnly="True" Header="Inversion" DataFormatString="{}{0:C}" 
                                                HeaderCellStyle="{StaticResource GridViewHeaderCellStyle}"
                                                CellStyle="{StaticResource GridViewCellStyle}"/>
    <telerik:GridViewDataColumn Width="80" DataMemberBinding="{Binding Ytd}" IsReadOnly="True" Header="YTD" DataFormatString="{}{0:C}" 
                                                HeaderCellStyle="{StaticResource GridViewHeaderCellStyle}"
                                                CellStyle="{StaticResource GridViewCellStyle}"/>
    <telerik:GridViewColumn Width="80" IsReadOnly="True" Header="Product" 
                                                HeaderCellStyle="{StaticResource GridViewHeaderCellStyle}" 
                                                CellStyle="{StaticResource GridViewCellStyle}" >

网格背后的代码。

void OnGridViewSelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
{
    var grid = sender as RadGridView;
    if (grid.SelectedItem != null)
    {
        grid.ScrollIntoView(grid.SelectedItem);
            var _internetDataViewModel = this.DataContext as InternetDataViewModel;
            _internetDataViewModel.ImagenSeleccionada = ((ExecutiveDashboard.TemperatureData)(grid.SelectedItem)).Image;
    }
}

什么不好?

纳克斯

4

1 回答 1

0

{Binding}不会自动知道在哪里定位ImagenSeleccionada

您需要将其链接到特定元素,通常是您的命名用户控件:

<UserControl x:Name="ThisCtrl" .... >
    <Image Source="{Binding ImagenSeleccionada, ElementName=ThisCtrl}" ... />

或将DataContext您的用户控件显式设置为自身;然后将DataContext向下传播到用户控件子项:

<UserControl DataContext="{Binding RelativeSource={RelativeSource Self}}" ... >
    <Image Source="{Binding ImagenSeleccionada}" ... />

(或者,您可以在代码隐藏构造函数中设置为,DataContextthis

或者您可以将图像绑定的相对源设置为作为用户控件的祖先,最好通过其类型:

<UserControl ...>
    <Image Source="{Binding ImagenSeleccionada,
           RelativeSource={RelativeSource AncestorType=local:YourUserControlType}}" ... />
于 2013-05-17T07:04:48.877 回答