Home > Software design >  How to pass an array or a list to a user control as a dependency property?
How to pass an array or a list to a user control as a dependency property?

Time:09-04

I have a user control named QuestionBox with has a text ,point and a list of options. My question view model:

 public long QuestionId { get; set; }
 public string Text { get; set; }
 public int Point { get; set; }
 public List<OptionViewModel> Options { get; set; }

So in user control I add these properties but it seems like the list does't work userControl.Xaml.cs

public string QuestionRoute
    {
        get { return (string)GetValue(QuestionRouteProperty); }
        set { SetValue(QuestionRouteProperty, value); }
    }

    // Using a DependencyProperty as the backing store for b 
QuestionRoute.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty QuestionRouteProperty =
        DependencyProperty.Register("QuestionRoute", typeof(string), 
  typeof(QuestionBox), new PropertyMetadata(string.Empty));


    public int Point
    {
        get { return (int)GetValue(PointProperty); }
        set { SetValue(PointProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Point.  
 This enables animation, styling, binding, etc...
    public static readonly DependencyProperty PointProperty =
        DependencyProperty.Register("Point", typeof(int), 
 typeof(QuestionBox), new PropertyMetadata(0));


    public List<OptionViewModel> Options
    {
        get { return 
 (List<OptionViewModel>)GetValue(OptionsProperty); }
        set { SetValue(OptionsProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Options.  
 This enables animation, styling, binding, etc...
    public static readonly DependencyProperty OptionsProperty =
        DependencyProperty.Register("Options", 
   typeof(List<OptionViewModel>), typeof(QuestionBox));

and here's the code that I pass the properties to

 public partial class Manager : Window
{
    public Manager()
    {
        InitializeComponent();
        var questions = QuestionList.GetAllQuestions();
        foreach (var item in questions)
        {
            QuestionBox questionBox = new QuestionBox();
            questionBox.QuestionRoute = "fff";
            questionBox.Point = 1;
            questionBox.Options = item.Options;
            Main.Children.Add(questionBox);
        }
    }

}

but It doesn't work and It's like there's no array that I passed. even when I checked it with break point everything was fine and the optionViewModel had 2 arguments!! I can show you the xaml or the class I fill the lists if u want.

here's the xaml of user control

    <Grid>
        <StackPanel>
            <TextBox Text="{Binding QuestionRoute}" Height="50"></TextBox>
            <ListView ItemsSource="{Binding Options}">
                <StackPanel Margin="0 10" FlowDirection="RightToLeft" Width="500" Height="50" Orientation="Horizontal">
                    <Grid Background="#fff" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown"  Tag="1"    Margin="20 0">
                        <Ellipse  Fill="#fff" Stroke="Black" Width="50" Height="50">                           
                        </Ellipse>
                        <Ellipse  Fill="Black" Width="0" Height="0">
                            
                        </Ellipse>
                    </Grid>
                    <TextBox Text="daha" Width="400"></TextBox>
                </StackPanel>
            </ListView>
        </StackPanel>
    </Grid>

and lists that I created

 public List<QuestionViewModel> Questions { get; set; } = 
GetAllQuestions();
    public static List<QuestionViewModel> GetAllQuestions()
    {
        List<QuestionViewModel> res = new List<QuestionViewModel>();
       
        int i;
        for ( i = 1; i<4;i  )
        {
            List<OptionViewModel> options = new List<OptionViewModel>();
            OptionViewModel option = new OptionViewModel
            {
                OptionId = 2 * i - 1,
                OptionText = "گزینه اول",
                QuestionId = i,
                IsCorrectAnswer = false,
            };
            OptionViewModel option2 = new OptionViewModel
            {
                OptionId = 2 * i,
                OptionText = "گزینه دوم",
                QuestionId = i,
                IsCorrectAnswer = false,
            };
            options.Add(option);
            options.Add(option2);
            var q = new QuestionViewModel
            {
                QuestionRoute = "dada",
                OrderId = i,
                Point = 1,
                Time = 20, 
                Options = options
            };
            res.Add(q);
            i  ;
        }
        return res;
    }

CodePudding user response:

You should wrap your ListView items with a <ListView.ItemTemplate/> like this:

<ListView ItemsSource="{Binding Options}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="0 10" FlowDirection="RightToLeft" Width="500" Height="50" Orientation="Horizontal">
                <Grid Background="#fff" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown"  Tag="1"    Margin="20 0">
                    <Ellipse  Fill="#fff" Stroke="Black" Width="50" Height="50">                           
                    </Ellipse>
                    <Ellipse  Fill="Black" Width="0" Height="0">
                        
                    </Ellipse>
                </Grid>
                <TextBox Text="daha" Width="400"></TextBox>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
  • Related