为列表框设置所选项目或所选索引包含绑定项目源WPF

时间:2016-03-06 21:04:41

标签: wpf xaml data-binding listbox converter

我是WPF的新手。我没有成功将选定的索引\选定值设置为列出图像项目框。

我目前的代码是:

关于xaml:

<ListBox Name="symbolSrc" Grid.Row="1" Width="485" Height="100" HorizontalAlignment="Left" Margin="7,-35,0,0" ItemTemplate="{StaticResource cmbTemplate}" IsSynchronizedWithCurrentItem="True" SelectedValuePath="photo">
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>
                            <ListBox.ItemContainerStyle>
                                <Style TargetType="ListBoxItem">
                                    <Setter Property="Padding" Value="6 0 0 0" />
                                </Style>
                            </ListBox.ItemContainerStyle>
                            <ListBox.ItemsSource>
                                <MultiBinding Converter="{StaticResource SymbolComboboxItemsFilter}">
                                    <Binding ElementName ="RadioButLR" Path="IsChecked" Mode="OneWay"/>
                                    <Binding ElementName ="RadioButHR" Path="IsChecked" Mode="OneWay"/>
                                </MultiBinding>
                            </ListBox.ItemsSource>
                            <ListBox.SelectedValue>
                                <MultiBinding Converter="{StaticResource SymbolComboboxSelectedItem}">
                                    <Binding ElementName ="RadioButLR" Path="IsChecked" Mode="OneWay"/>
                                    <Binding ElementName ="RadioButHR" Path="IsChecked" Mode="OneWay"/>
                                </MultiBinding>
                            </ListBox.SelectedValue>
</ListBox>

在xaml.cs上:

public class SymbolComboboxItemsFilterConverter: IMultiValueConverter
{
    public class symbolCmbBoxItem
    {
        public string photo { get; set; }
        public string photoName { get; set; }
    }
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        List<symbolCmbBoxItem> itemsList = new List<symbolCmbBoxItem>();
        string targetDir = System.Windows.Forms.Application.ExecutablePath + "\\..\\..\\..\\INIT\\IC";

        string[] filesArray = Directory.GetFiles(targetDir, "*", SearchOption.TopDirectoryOnly);

        if ((bool)values[0])
        {
            foreach (string file in filesArray.Where(s => s.Contains("_LR")))
            {
                itemsList.Add(new symbolCmbBoxItem { photo = file, photoName = "" });
            }
        }
        else
        {
            foreach (string file in filesArray.Where(s => s.Contains("_HR")))
            {
                itemsList.Add(new symbolCmbBoxItem { photo = file, photoName = "" });
            }
        }

        return itemsList;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public class SymbolComboboxItemsSelectedIndex : IMultiValueConverter
{
    public class symbolCmbBoxItem
    {
        public string photo { get; set; }
        public string photoName { get; set; }
    }
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        List<symbolCmbBoxItem> itemsList = new List<symbolCmbBoxItem>();
        string targetDir = System.Windows.Forms.Application.ExecutablePath + "\\..\\..\\..\\INIT\\IC";

        string[] filesArray = Directory.GetFiles(targetDir, "*", SearchOption.TopDirectoryOnly);

        if ((bool)values[0])
        {
            string item = filesArray.Where(s => s.Contains("_LR")).First();
            return new symbolCmbBoxItem { photo = item, photoName = "" };
        }
        else
        {
            string item = filesArray.Where(s => s.Contains("_HR")).First();
            return new symbolCmbBoxItem { photo = item, photoName = "" };
        }
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我还尝试使用以下方法设置所选项目:

的SelectedItem =&#34; 1&#34;

它没有用。

还尝试在上面使用相同的多转换器,并始终将所选项目设置为&#34; 1&#34;它也没有用。

(为了设置我在下面参考时使用的图像列表框:

http://www.codescratcher.com/wpf/wpf-combobox-with-image/

有什么想法吗?

非常感谢您的帮助!

0 个答案:

没有答案
相关问题