MahApps TabControl绑定

时间:2018-02-23 15:24:50

标签: c# wpf data-binding mvvm-light mahapps.metro

我非常喜欢wpf,即时通讯使用MVVM灯。 我有一个视图与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>

1 个答案:

答案 0 :(得分:1)

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

它将自动继承TickerViewModel中与TabControl相对应的DataContext