簡體   English   中英

MahApps TabControl綁定

[英]MahApps TabControl Binding

您好,我使用wpf非常新,我使用MVVM light。 我有一個視圖,其中Mahapps選項卡控件綁定到主視圖的viewmodel中的列表,我有一個按鈕,可將項目添加到列表中。 mahapps tabitem綁定到內容視圖,但是由於某些原因,即使我將項目添加到綁定列表時它也不顯示任何內容,它也添加了一個新的選項卡項目。 可能我做錯了任何建議。 提前致謝

TickersView

<UserControl x:Class="V2.Views.TickersView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
         xmlns:local="clr-namespace:V2.Views"
         xmlns:y="clr-namespace:V2.ViewModel"
         xmlns:simpleChildWindow="clr-namespace:MahApps.Metro.SimpleChildWindow;assembly=MahApps.Metro.SimpleChildWindow"


         mc:Ignorable="d"             
         d:DesignHeight="800" d:DesignWidth="600"
         DataContext="{Binding Tickers, Source={StaticResource Locator}}">




<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="15*"/>
    </Grid.RowDefinitions>
    <Button   Style="{StaticResource MaterialDesignFloatingActionMiniDarkButton}"  Content="+" VerticalContentAlignment="Center" Command="{Binding AddTickerCommand , Mode=OneWay}" HorizontalAlignment="Right" VerticalAlignment="Center"  Grid.Row="0" Margin="0,0,15,0"  BorderBrush="{x:Null}"  />
    <Controls:MetroAnimatedTabControl
        ItemsSource="{Binding TickersList}"

        Grid.Row="1">

        <Controls:MetroAnimatedTabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding Name}"/>
            </Style>
        </Controls:MetroAnimatedTabControl.ItemContainerStyle>
        <Controls:MetroAnimatedTabControl.ContentTemplate>
            <DataTemplate DataType="{x:Type y:TickerViewModel}">
                <ContentControl Content="{Binding TickerView}"  Height="{Binding ActualHeight, ElementName=parentElementName}" Width="{Binding ActualWidth, ElementName=parentElementName}" />
            </DataTemplate>
        </Controls:MetroAnimatedTabControl.ContentTemplate>

    </Controls:MetroAnimatedTabControl>



</Grid>

TickersViewModel

private ObservableCollection<TickerViewModel> _tickers = new ObservableCollection<TickerViewModel>();
private Market _selectedMarket;

public ObservableCollection<TickerViewModel> TickersList
{
    get
    {
        return _tickers;
    }
    set
    {
        Set(ref _tickers, value);
    }
}

TickerViewModel

public class TickerViewModel : ViewModelBase
{
    public string Name { get; set; }
    public Market Market { get; set; }
    public Exchange SelectedExchange{ get; set; }
    public MarketSummary Summary { get; set; }


}

TickerView

<UserControl x:Class="V2.Views.TickerView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:V2.Views"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         DataContext="{Binding Ticker, Source={StaticResource Locator}}">
<Grid>
    <StackPanel>
        <TextBlock Text="{Binding Name}"/>
    <Button Height="Auto" Margin="0,118,0,134">sdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasda</Button>
        <TextBlock Text="{Binding Market}"/>
    </StackPanel>
</Grid>

ContentTemplate應該包含一個TickerView ,而不是綁定到名為TickerView的屬性:

<Controls:MetroAnimatedTabControl.ContentTemplate>
    <DataTemplate DataType="{x:Type y:TickerViewModel}">
        <local:TickerView />
    </DataTemplate>
</Controls:MetroAnimatedTabControl.ContentTemplate>

並從TickerView

DataContext="{Binding Ticker, Source={StaticResource Locator}}">

它將自動繼承TabControl的相應TickerViewModel作為其DataContext

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM