简体   繁体   English

使用模板列表框项将可观察列表绑定到列表框

[英]binding observable list to listbox with templete listbox items

I have the following templete我有以下模板

<ControlTemplate x:Key="tmpl_btn_TecnicianModeMenu" TargetType="{x:Type ListBoxItem}">
<Grid Opacity="1" d:LayoutOverrides="Width, Height">
    <Border 
        x:Name="Border"  
        CornerRadius="0" 
        BorderThickness="0" Height="Auto" Margin="0" Background="White">

        <StackPanel Name="stackPanel" Height="Auto" Margin="0" Orientation="Horizontal" >
            <Button x:Name="button" Style="{DynamicResource ButtonListBoxItem}" Margin="5,5,5,5" Width="120" Height="Auto" BorderThickness="0" >
                <TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="בצע"  Margin="12,0" VerticalAlignment="Center" HorizontalAlignment="Stretch" Style="{DynamicResource tb_Desc}"/>
            </Button>
            <StackPanel Height="Auto" Margin="0" Orientation="Horizontal" >
                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" x:Name ="LB_Result"  Text="LB_Result" Style="{DynamicResource LB_AreaTitle_Balance}" Margin="5,5,5,5" d:LayoutOverrides="Height" />
                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" x:Name ="LB_OK"  Text="LB_OK" Style="{DynamicResource LB_AreaTitle_Balance}" Margin="5,5,5,5" d:LayoutOverrides="Height" />
                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" x:Name ="LB_TchName"  Text="LB_TchName" Style="{DynamicResource LB_AreaTitle_Balance}" Margin="5,5,5,5"/>
                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" x:Name ="LB_Date"  Text="LB_Date" Style="{DynamicResource LB_AreaTitle_Balance}" Margin="5,5,5,5" d:LayoutOverrides="Height"/>
                <TextBlock HorizontalAlignment="Right" VerticalAlignment="Center" x:Name ="LB_CheckName"  Text="{TemplateBinding Tag}"   Style="{DynamicResource LB_AreaTitle_Balance}" Margin="5,5,5,5"/>
            </StackPanel>
        </StackPanel>
    </Border>
    <Border x:Name="Divide" BorderBrush="Gray" BorderThickness="0,0.5" Height="140" Width="Auto" Margin="18.5,0" VerticalAlignment="Bottom"/>
</Grid>
<ControlTemplate.Triggers>
    <Trigger Property="IsKeyboardFocused" Value="True"/>

    <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
    </Trigger>
</ControlTemplate.Triggers>

** I have list box of such items. ** 我有此类项目的列表框。 I want to bind it to an observable list.我想将它绑定到一个可观察列表。 the problem is that the previus programer added items to the list like this:**问题是 previus 程序员将项目添加到列表中,如下所示:**

  private void AddButtonToList(String LB_CheckName, String LB_Date, String LB_TchName, String LB_OK, String LB_Result, enmTechMode_Check enmCheck )
    {
        try
        {
            //create item
            ListBoxItem item2 = new ListBoxItem();
            //set template
            item2.SetResourceReference(ListBoxItem.TemplateProperty, "tmpl_btn_TecnicianModeMenu");
            item2.ApplyTemplate();

            item2.Height = 45;

            TextBlock txt1 = (TextBlock)item2.Template.FindName("LB_CheckName", item2); 
            txt1.Text = LB_CheckName;

            txt1 = (TextBlock)item2.Template.FindName("LB_Date", item2); 
            txt1.Text = LB_Date;

            txt1 = (TextBlock)item2.Template.FindName("LB_TchName", item2);  
            txt1.Text = LB_TchName;

            txt1 = (TextBlock)item2.Template.FindName("LB_OK", item2);   
            txt1.Text = LB_OK;

            txt1 = (TextBlock)item2.Template.FindName("LB_Result", item2);   
            txt1.Text = LB_Result;

            Button bt = (Button)item2.Template.FindName("button", item2);
            bt.SetResourceReference(Button.StyleProperty, "ButtonListBoxItem");
            bt.ApplyTemplate();

            bt.Click += new RoutedEventHandler(Item_Selected);
            //set tag
            bt.Tag = enmCheck;

            //add to list
            StackPanel sp = (StackPanel)ListBoxData.FindName("stackPanel");
            item2.Tag = enmCheck;
            sp.Children.Add(item2);

        }
        catch (Exception exp)
        {
         }
    }

and I have no clue how to fix this我不知道如何解决这个问题

I assume theres suppose to be a use of a convertor?我假设应该使用转换器? please provide me a direction请给我一个方向

I assume the observable list suppose to be of structs.我假设可观察列表应该是结构的。 but how convert those to items in the temples format?但是如何将它们转换为寺庙格式的项目呢?

In a template you would usually bind to properties of the data object. If for example you have the data class below with a Result property在模板中,您通常会绑定到数据 object 的属性。例如,如果您有下面的数据 class 和Result属性

public class MyData
{
    public string Result { get; set; }
    ...
}

you would bind like this:你会像这样绑定:

<TextBlock Text="{Binding Path=Result}" ... />

Then you would not manually add ListBoxItems, but instead add data objects to the ObservableCollection, that the ItemsSource property of the ListBox is bound to:那么您就不会手动添加 ListBoxItems,而是将数据对象添加到 ObservableCollection,ListBox 的ItemsSource属性绑定到:

myDataObjects.Add(
    new MyData
    {
        Result = "A Result"
        ...
    });

where myDataObjects is an ObservableCollection<MyData> .其中myDataObjects是一个ObservableCollection<MyData>

In case you would need to get the UI updated when a data object changes which is already contained in the ObservableCollection, class MyData would need to implement INotifyPropertyChanged .如果您需要在 ObservableCollection 中已包含的数据 object 更改时更新 UI,则 class MyData需要实施INotifyPropertyChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM