将可观察列表绑定到带有模板列表框项目的列表框

时间:2012-04-04 06:55:35

标签: c# wpf xaml

我有以下模板

<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>

**我有这些项目的列表框。 我想将它绑定到一个可观察的列表。 问题是,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)
        {
         }
    }

,我不知道如何解决此问题

我认为假设是使用转换器?请给我一个指示

我假设可观察列表假设是结构。但是如何将它们转换为太阳穴格式的物品?

1 个答案:

答案 0 :(得分:1)

在模板中,您通常会bind to properties数据对象。例如,如果您的数据类具有Result属性

public class MyData
{
    public string Result { get; set; }
    ...
}
你会像这样绑定:

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

然后你不会手动添加ListBoxItems,而是将数据对象添加到ObservableCollection中,ListBox的ItemsSource属性绑定到:

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

其中myDataObjectsObservableCollection<MyData>

如果您需要在ObservableCollection中已包含的数据对象发生更改时更新UI,则类MyData需要实现INotifyPropertyChanged