MVVMパターンに従っていますが、ListView ItemsSourceをXAMLを使用してバインドしたい場合でも、 this.Datacontext = ObservableCollectionプロパティではバインドしません。コードビハインドにないXAMLを使用してListView ItemsSourceをバインドする方法。
私のコードは次のようである:
<ListView x:Name="MenuBarList"
Grid.Row="2"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding Path=Menu.Option}"
Width="{Binding MainMenuWidth}"
SelectedItem="{Binding Path=SelectedMainMenuOption, Mode=TwoWay}" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" IsHitTestVisible="False" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
とメニューが財産であり、それはViewModelにに着座します。オプションはプロパティのクラスなので、Menu.Optionを使用しています
マイメニューはContentMenuModel型のプロパティで、ContentMenuModelはOption、Title、Imageのプロパティを含むクラスです。
のViewModel
public const string MenuPropertyName = "Menu";
private ContentMenuModel _Menu = null;
/// <summary>
/// Gets the Menu collection.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public ContentMenuModel Menu
{
get
{
return _Menu;
}
set
{
if (_Menu == value)
return;
_Menu = value;
// Update bindings, no broadcast
RaisePropertyChanged(MenuPropertyName);
}
}
そしてContentMenuModelクラスの内部にあるメニューのプロパティを参照してくださいすることは次のようになります。
public class ContentMenuModel
{
#region Title
/// <summary>
/// The <see cref="Title" /> property's name.
/// </summary>
public const string TitlePropertyName = "Title";
private string _Title = String.Empty;
/// <summary>
/// Gets the Title property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
[Required]
[StringLength(128, ErrorMessage = "The Title value cannot exceed 128 characters. ")]
public string Title
{
get
{
return _Title;
}
set
{
if (_Title == value)
{
return;
}
var oldValue = _Title;
_Title = value;
// Update bindings, no broadcast
RaisePropertyChanged(TitlePropertyName);
}
}
#endregion
#region Options
/// <summary>
/// The <see cref="Options" /> property's name.
/// </summary>
public const string OptionsPropertyName = "Options";
private ObservableCollection<ContentMenuOptionModel> _Options = null;
/// <summary>
/// Gets the Options property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public ObservableCollection<ContentMenuOptionModel> Options
{
get
{
return _Options;
}
set
{
if (_Options == value)
{
return;
}
var oldValue = _Options;
_Options = value;
RaisePropertyChanged(OptionsPropertyName);
}
}
#endregion
#region ContextText
/// <summary>
/// The <see cref="Options" /> property's name.
/// </summary>
public const string ContextTextPropertyName = "ContextText";
private ContentPageItem _ContextText = null;
/// <summary>
/// Gets the ContextText property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public ContentPageItem ContextText
{
get
{
return _ContextText;
}
set
{
if (_ContextText == value)
{
return;
}
_ContextText = value;
RaisePropertyChanged(OptionsPropertyName);
}
}
#endregion
}
が、私は私のメインウィンドウのDataContextのとパスにViewModelLocatorをバインドさていました= ViewModelのMainMenu、MainMainは、このプロパティをListViewのItemsSourceにバインドできるViewModelのオブジェクトですが、機能しません。
私が間違っている場所を修正してください。
こんにちはVladimr私はあなたが私に説明したが、それは私が以前にも私はRadCarouselコントロールを持っていたと私は結果を与えていないと完全に動作していたが、リストビューが動作していません。 –
OptionクラスとMenuクラスを表示し、どのように表示されるかを示します。 –
私のコードを修正しているだけで、私の嘆願を修正しています。ありがとうございました。 –