0

我有以下模板

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

** 我有此类项目的列表框。我想将它绑定到一个可观察的列表。问题是以前的程序员将项目添加到列表中,如下所示:**

  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)
        {
         }
    }

我不知道如何解决这个问题

我假设theres假设是使用转换器?请给我一个方向

我假设可观察列表应该是结构。但是如何将它们转换为寺庙格式的项目?

4

1 回答 1

1

在模板中,您通常会绑定到数据对象的属性。例如,如果您有下面带有Result属性的数据类

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

你会像这样绑定:

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

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

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

哪里myDataObjectsObservableCollection<MyData>

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

于 2012-04-04T07:51:17.747 回答