绑定组合框图像 - 如何选择组合框图像中保存的项目

时间:2014-06-25 09:47:32

标签: c# wpf xaml combobox

我在wpf和xaml上遇到了ComboBox的问题。我的组合框的ItemsSource是一个(CollarTypesImage)列表。 绑定工作正常,但编辑数据时的问题,我无法选择我的ComboBox的项目..也许它的选择,但图像不是视图...

但是当点击ComboBox时,我可以看到所有的帖子:

http://www.ahmadabouhamdh.com/tmp_global/1.png

我使用了RelativeSource,没有任何改变,ComboBox没有选择我保存的项目:

<UserControl x:Class="TailorManager.Views.OrderDetailItem"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:vm="clr-namespace:TailorManager.ViewModels" 
  xmlns:Converter="clr-namespace:TailorManager.Converters"
         mc:Ignorable="d" 
         d:DesignHeight="730" d:DesignWidth="556" FlowDirection="RightToLeft" >
<UserControl.Resources>
    <!--<vm:OrderDetailItemViewModel x:Key="OrderDetailItemViewModel1" />-->
    <Converter:ImageConverter x:Key="ImgConverter" />
</UserControl.Resources>
<Grid >
    <ScrollViewer VerticalScrollBarVisibility="Auto" >
        <ItemsControl>
            <StackPanel> 
                <GroupBox Header="تفاصيل الياقة" Margin="5,5,5,0" VerticalAlignment="Top" Height="170">
                    <Grid>
                       <ComboBox ItemsSource="{Binding Path= DataContext.ImagesCollarTypes,
                            RelativeSource={RelativeSource AncestorType=UserControl,AncestorLevel=1}}" 
                                  SelectedValue="{Binding Path=OrderDetailItem.CollarTypesImage,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  
                                  SelectedValuePath="{Binding Path=OrderDetailItem.CollarTypesImage}"
                                  Margin="393,106,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="132" Height="38">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <Image Width="50" Height="50" Source="{Binding Path= CollarImage,Converter={StaticResource ImgConverter} }" />
                                </DataTemplate>
                            </ComboBox.ItemTemplate >
                        </ComboBox>

                    </Grid>
                </GroupBox> 
                <Grid/>
            </StackPanel>

        </ItemsControl>
    </ScrollViewer>
</Grid>
</UserControl>

我添加了

 SelectedItem="{Binding Path=OrderDetailItem.CollarTypesImage}"

并没有改变。

我使用此控件内部窗口具有不同的DataContext类型,我从此控件的构造函数设置(DataContext OrderDetailItem)的值。

下面的图片说明绑定工作正常:

此图像单击编辑以显示我从DB保存的图像,我的项目未选中:

http://www.ahmadabouhamdh.com/tmp_global/2.png

这里是所有代码:

    public List<CollarTypesImage> ImagesCollarTypes
    {
        get
        {
            //ImagesCollarTypes[0].CollarImage
            if (_imagesCollarTypes.Count == 0)
            {
                TailorManagerDBEntities db = new TailorManagerDBEntities();
                _imagesCollarTypes = db.CollarTypesImages.ToList();
            }
            return _imagesCollarTypes;
        }
        set
        {
            _imagesCollarTypes = value;
            RaisePropertyChanged(() => ImagesCollarTypes);
        }
    }

public partial class CollarTypesImage
{
    public CollarTypesImage()
    {
        this.OrderDetails = new HashSet<OrderDetail>();
    }

    public System.Guid CollarTypeId { get; set; }
    public byte[] CollarImage { get; set; }

    public virtual ICollection<OrderDetail> OrderDetails { get; set; }
}

这里来自窗口的xaml代码我使用了我的控件:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:vm="clr-namespace:TailorManager.ViewModels"
    xmlns:util="clr-namespace:TailorManagerLib.Business;assembly=TailorManagerLib"
    xmlns:local="clr-namespace:TailorManager.Views"
x:Class="TailorManager.Views.AddOrder"
    Title="AddCustomer" Height="665" Width="974" MinWidth="600" MinHeight="666" FlowDirection="RightToLeft" >
<Window.Resources>
    <vm:ManageOrderDetilsViewModel x:Key="ManageOrderDetilsViewModel1" />
</Window.Resources>
<Grid DataContext="{StaticResource ManageOrderDetilsViewModel1}" x:Name="GridDataContaner">
    <Grid.Background>
       ...
    </Grid.Background>

    <GroupBox Header="تفاصيل الطلب" Margin="10,160,10,0" FlowDirection="RightToLeft">
        <Grid Grid.Column="0" Name="GridOrderDetails">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Name="ColumnDefinitionListDetails" Width="183*"/>
                <ColumnDefinition Name="ColumnDefinitionDetails" Width="0*"/>
            </Grid.ColumnDefinitions>
            <Grid Margin="0,0,0,40" >
               ...
            </Grid>
            <Grid Grid.Column="1">
                <Border BorderBrush="Black" BorderThickness="2" CornerRadius="3" Margin="4" >
                    <Grid>
                       <local:OrderDetailItem x:Name="OrderDetailItemControl" VerticalAlignment="Top" />
                    </Grid>
                </Border>

            </Grid>
            ...
        </Grid>
    </GroupBox>

</Grid>
</Window>

我如何设置Control的DataContext,这是我窗口VM中命令的内容:

OrderDetailItemViewModel OrdDetailItem = new OrderDetailItemViewModel(Ord.OrderDetailsId);
OrderDetailItemControl.DataContext = OrdDetailItem;

请问如何解决?


  
    
      

更新

    
  

当我更改相关来源时如下:

<ComboBox Name="CmbBxImgCollarTyp" ItemsSource="{Binding Path= DataContext.ImagesCollarTypes, RelativeSource={
RelativeSource AncestorType={x:Type vm:OrderDetailItemViewModel}}" 
SelectedValue="{Binding Path=OrderDetailItem.CollarTypesImage, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="{Binding Path= OrderDetailItem.CollarTypesImage}" Margin="393,106,0,0" VerticalAlignment="Top" 
HorizontalAlignment="Left" Width="132" Height="38">
                            <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <Image Width="50" Height="50" Source="{Binding Path=CollarImage,
            Converter={StaticResource ImgConverter}}" />
                                </DataTemplate>
                            </ComboBox.ItemTemplate>
                        </ComboBox>

绑定永远不会在所有模式下工作!,我将其更改为这种方式导致此控件的datacontext是viewmodel“OrderDetailItemViewModel”的对象 应该修复什么?

2 个答案:

答案 0 :(得分:0)

RelativeSource Binding Path出现错误。您使用的是UserControl类型,但UserControl有一个名为ImagesCollarTypes的属性...相反,您应该使用您的 UserControl您定义了该属性...我假设该类名为OrderDetailItem

<ComboBox ItemsSource="{Binding Path= DataContext.ImagesCollarTypes, RelativeSource={
    RelativeSource AncestorType={x:Type YourViewsPrefix:OrderDetailItem}}}" 
    SelectedValue="{Binding Path=CollarTypesImage, Mode=TwoWay,
    UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="{Binding Path=
    CollarTypesImage}" Margin="393,106,0,0" VerticalAlignment="Top" 
    HorizontalAlignment="Left" Width="132" Height="38">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Image Width="50" Height="50" Source="{Binding Path=CollarImage,
                Converter={StaticResource ImgConverter}}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

您应该在Visual Studio的“输出”窗口中收到错误,其中包含错误:属性&#39; ImagesCollarTypes&#39;找不到对象&#39; UserControl&#39; 或类似内容。

  

免责声明:我没有查看您的所有代码,因为您的代码太多,所以即使您修复了此错误,您的代码仍可能出现其他错误而无法正常工作。


更新&gt;&gt;&gt;

为了使这个Binding起作用,做出了以下假设:

  1. UserControl.DataContext必须具有对象集的有效实例。
  2. 设置为UserControl.DataContext的对象必须具有名为ImagesCollarTypes的公共属性。
  3. ImagesCollarTypes属性必须是包含名为CollarTypesImage的公共属性的类型。

答案 1 :(得分:0)

检查输出窗口后,我注意到这个错误:

System.Windows.Data Error: 4 : Cannot find source for binding with 
reference 'RelativeSource FindAncestor, AncestorType='Projectname.ViewModels.ViewModel', AncestorLevel='1''. BindingExpression:Path=LookupItems; DataItem=null; target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

然后我将RelativeSource从AncestorType从我的ViewModel更改为UserControl,就像这样:  RelativeSource={ AncestorType={x:Type UserControl},AncestorLevel=1}}"

现在一切正常,

感谢会员努力