منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : كيفية جلب ال SelectedValue ل ComboBox داخل UserControl
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
يعطيكم العافية شباب ...
لقد قمت بعمل UserControl بداخلة ComboBox يتم تعبئة من قاعدة البيانات باستخدام ال Entity Framework وذلك لاختصار الكود حيث كلما كنت بحاجة له ادرجه ضمن النافذة وبذلك يكتب الكود مرة واحدة
لكن واجهتني مشكلة بكيفية عمل Binding لقيمة ال SelectedValue عند استخدام ال UserControl  في اي نافذة

اليكم ما قمت به
أولاً : كود ال  (XAML)UserControl
كود :
<UserControl
   x:Class="QudsVoiceAdvertisement.UserControls.CustomerTypeComboBox"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   xmlns:model="clr-namespace:DAL.Model;assembly=DAL"
   mc:Ignorable="d"
   d:DesignHeight="50"
   d:DesignWidth="100"
   FlowDirection="RightToLeft"
   Loaded="CustomerTypeComboBox_OnLoaded">

   <ComboBox
       Name="MyComboBox"
       DisplayMemberPath="CustomerTypeName"
       IsEditable="True"
       materialDesign:HintAssist.Hint="تصنيف الموظف"
       materialDesign:HintAssist.IsFloating="True"
       SelectedValue="{Binding Path = UserSelectedValue ,Mode=OneWayToSource , UpdateSourceTrigger=PropertyChanged }"
       SelectedValuePath="Id" />
</UserControl>

ثانيا: عمل DependencyProperty تسند اليها قيمة ال SelectedValue
كود :
public object UserSelectedValue
       {
           get => (int)GetValue(UserSelectedValueProperty);
           set => SetValue(UserSelectedValueProperty , value);
       }

       // Using a DependencyProperty as the backing store for SelectedValu...  
       public static readonly DependencyProperty UserSelectedValueProperty =
           DependencyProperty.Register("UserSelectedValue", typeof(int), typeof(CustomerTypeComboBox), new PropertyMetadata(0));



ثالثاا : استخدام ال UserControl ضمن النافذ حيث قمت بعمل DependencyProperty لكن فشل في جلب قيمة ال SelectedValue
كود :
           <userControls:CustomerTypeComboBox
               x:Name="ComboBoxCustomrType"
               UserSelectedValue ="{Binding CustomerTypeId , Mode=TwoWay , UpdateSourceTrigger=PropertyChanged}"
               Margin="0 10 0 0"
               Width="150"
               HorizontalAlignment="Left" />


شكله بغ في الفيجوال ستوديو:

جرب القيام بالتالي : 

  1. Close your XAML files.

  2. Clean your Solution.

  3. Close Visual Studio.

  4. Restart your PC. Yes, this seems like overkill, but it was part of worked, so I'm including it.

  5. Re-open your solution and rebuild.

  6. Re-open your XAML file and the problem should be fixed.
وايضا public object UserSelectedValue
 يجب ان يكون
 public int UserSelectedValue
it's doesn't work Sad