C# WPF组合框项目资源不工作

C# WPF组合框项目资源不工作,c#,wpf,C#,Wpf,我是WPF的新手,正在尝试修复现有的程序。 有一个组合框定义为: <ComboBox Height="23" Margin="111.5,6.738,6,0" Name="comboBoxDocType" VerticalAlignment="Top" FontFamily="Calibri" FontSize="11" SelectedIndex="0" SelectionChanged="comboBoxDocType_SelectionChanged" ItemsSource="{B

我是WPF的新手,正在尝试修复现有的程序。 有一个组合框定义为:

<ComboBox Height="23" Margin="111.5,6.738,6,0" Name="comboBoxDocType" VerticalAlignment="Top" FontFamily="Calibri" FontSize="11" SelectedIndex="0" SelectionChanged="comboBoxDocType_SelectionChanged" ItemsSource="{Binding}">

尝试在代码隐藏中填充它:

DocumentTypesList = new List<string>();
DocumentTypesList.Add(DocumentTypes.Unknown);  
DocumentTypesList.Add(DocumentTypes.PurchaseOrder);
DocumentTypesList.Add(DocumentTypes.RMInvoice);
DocumentTypesList.Add(DocumentTypes.SundryOne);
DocumentTypesList.Add(DocumentTypes.DevelopmentPaper);
comboBoxDocType.ItemsSource = DocumentTypesList;
DocumentTypesList=newlist();
DocumentTypesList.Add(DocumentTypes.Unknown);
DocumentTypesList.Add(DocumentTypes.PurchaseOrder);
DocumentTypesList.Add(DocumentTypes.Voice);
DocumentTypesList.Add(DocumentTypes.SundryOne);
DocumentTypesList.Add(DocumentTypes.DevelopmentPaper);
comboBoxDocType.ItemsSource=DocumentTypesList;

组合框中没有任何内容。有什么东西不见了吗

ItemsSource=“{Binding}”
在此上下文中是不必要的。

ItemsSource=“{Binding}”
在此上下文中是不必要的。

这可以通过纯绑定完成。我不太确定您试图实现什么,但下面的示例展示了如何抓住事件并避免代码隐藏。您也可以绑定到SelectedItem。然而,这里有一个解决你问题的建议

Xaml:


请注意,我在这里使用的是viewmodel,所以必须将视图的datacontext设置为此类的实例。就我个人而言,如果可能的话,我会避免这个事件,而只是绑定到selecteditem,或者创建一个行为,以避免这种耦合

代码:

public类YourViewModel:INotifyPropertyChanged
private ObservableCollection documentTypesList=新的ObservableCollection{DocumentTypes.Unknown,DocumentTypes.PurchaseOrder,DocumentTypes.RMInvoice,DocumentTypes.SundryOne,DocumentTypes.DevelopmentPaper};
公共可观察收集文档类型列表
{
获取{return documentTypesList;}
设置
{
if(等于(value,documentTypesList))返回;
documentTypesList=值;
OnPropertyChanged();
}
}
公共ICommand DocumentSelectionChangedCommand{get;set;}
公共视图模型()
{
InitStuff();
}
公共物品{
DocumentSelectionChangedCommand=新的RelayCommand(OnDocumentChanged);
}
文档已更改的私有void(选择ChangedEventArgs e)
{
//到您的东西在这里,但所有这一切都可以通过绑定以及!
//在某些SelectedDocuments属性的setter或其他内容中调用
}
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]//如果没有R,则注释掉此行#
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
希望有帮助

干杯


Stian

这可以通过纯绑定完成。我不太确定您试图实现什么,但下面的示例展示了如何抓住事件并避免代码隐藏。您也可以绑定到SelectedItem。然而,这里有一个解决你问题的建议

Xaml:


请注意,我在这里使用的是viewmodel,所以必须将视图的datacontext设置为此类的实例。就我个人而言,如果可能的话,我会避免这个事件,而只是绑定到selecteditem,或者创建一个行为,以避免这种耦合

代码:

public类YourViewModel:INotifyPropertyChanged
private ObservableCollection documentTypesList=新的ObservableCollection{DocumentTypes.Unknown,DocumentTypes.PurchaseOrder,DocumentTypes.RMInvoice,DocumentTypes.SundryOne,DocumentTypes.DevelopmentPaper};
公共可观察收集文档类型列表
{
获取{return documentTypesList;}
设置
{
if(等于(value,documentTypesList))返回;
documentTypesList=值;
OnPropertyChanged();
}
}
公共ICommand DocumentSelectionChangedCommand{get;set;}
公共视图模型()
{
InitStuff();
}
公共物品{
DocumentSelectionChangedCommand=新的RelayCommand(OnDocumentChanged);
}
文档已更改的私有void(选择ChangedEventArgs e)
{
//到您的东西在这里,但所有这一切都可以通过绑定以及!
//在某些SelectedDocuments属性的setter或其他内容中调用
}
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]//如果没有R,则注释掉此行#
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
}
希望有帮助

干杯

Stian

公共主页()
{
this.InitializeComponent();
//获取菜单
列表菜单\列表=加载\菜单();
lst_menu.itemsource=菜单列表;
}
私人导航助手导航助手;
private observedictionary DefaultViewmodel=new observedictionary();
私有字符串[]Logo_menu_array={“Assets/star-6-48.ico”、“/Assets/note-48.ico”、“/Assets/medal-48.ico”、“/Assets/mogage-48.ico”};
私有字符串[]文本菜单数组={“Ph”ổ 毕ế新罕布什尔州ạc、Thể "thao","Tròchơi",;
//菜单
公共类listboxitem
{
公共字符串文本菜单{get;set;}
公共字符串logomenu{get;set;}
}
//加载菜单
公共列表加载菜单()
{
列表文本=新列表();
对于(int i=0;i<ComboBox Height="23" Margin="111.5,6.738,6,0"  VerticalAlignment="Top" FontFamily="Calibri" FontSize="11" SelectedIndex="0"  ItemsSource="{Binding DocumentTypesList}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <command:EventToCommand Command="{Binding DocumentSelectionChangedCommand}" PassEventArgsToCommand="True"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>
public class YourViewModel : INotifyPropertyChanged

    private ObservableCollection<DocumentTypes> documentTypesList = new ObservableCollection<DocumentTypes> {DocumentTypes.Unknown, DocumentTypes.PurchaseOrder, DocumentTypes.RMInvoice, DocumentTypes.SundryOne, DocumentTypes.DevelopmentPaper};       

    public ObservableCollection<DocumentTypes> DocumentTypesList
    {
       get { return documentTypesList; }
       set
       {
           if (Equals(value, documentTypesList)) return;
           documentTypesList = value;
           OnPropertyChanged();
       }
    }

    public ICommand DocumentSelectionChangedCommand { get; set; }


    public YourViewModel() 
    {
        InitStuff();
    }

    public void InitStuff(){
        DocumentSelectionChangedCommand = new RelayCommand<SelectionChangedEventArgs>(OnDocumentChanged);
    }

    private void OnDocumentChanged(SelectionChangedEventArgs e)
    {
        // To your stuff here, but all this can be done by bindings as well!
        // Invoke in some SelectedDocuments property's setter or something
    }


    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator] // Comment out this line if no R#
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
 }
  public MainPage()
    {
        this.InitializeComponent();
        //get menu
         List<listboxitem> menu_list = Load_Menu();
         lst_menu.ItemsSource = menu_list;


    }


    private NavigationHelper NavigationHelper;
    private ObservableDictionary DefaultViewmodel = new ObservableDictionary();
    private string[] Logo_menu_array = { "Assets/star-6-48.ico", "/Assets/note-48.ico", "/Assets/medal-48.ico", "/Assets/joystick-48.ico" };
    private string[] Text_menu_array={"Phổ biến trên YouTuBe","Âm nhạc","Thể thao","Trò chơi"};






    //Menu
    public class listboxitem
    {
        public string textmenu { get; set; }
        public string logomenu { get; set; }
    }
    //load menu
    public List<listboxitem> Load_Menu()
    {
        List<listboxitem> text = new List<listboxitem>();
        for (int i = 0; i < Math.Min(Logo_menu_array.Length, Text_menu_array.Length); i++)
        {
            var l = new listboxitem();
            l.logomenu = Logo_menu_array[i];
            l.textmenu = Text_menu_array[i];
            text.Add(l);
        }

        return text;
    }