具有依赖项属性的WPF用户控件不起作用

时间:2016-03-01 21:37:28

标签: c# wpf user-controls dependency-properties

我正在编写一个WPF应用程序,并且有一个用户控件,其中包含一些我希望在多个地方使用的数据网格。在每个地方,我希望字体大小不同。

以下是我正在尝试的内容:

的.xaml:

<UserControl x:Class="RoviPutt.Controls.Leaderboard"
      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" 
      mc:Ignorable="d" 
      d:DesignHeight="605" d:DesignWidth="890">
    <UserControl.Resources>
        <Style TargetType="{x:Type DataGrid}" BasedOn="{StaticResource MetroDataGrid}">
            <Setter Property="FontSize" Value="{Binding TextSize}"/>
        </Style>
    </UserControl.Resources>
    <Grid>
        <ma:FlipView IsBannerEnabled="False">
            <ma:FlipView.Items>
                <Grid>
                    <DataGrid ItemsSource="{Binding ShowOverall.Leaders}" Margin="50" BorderThickness="2">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="RANK" Binding="{Binding Rank}" Width="Auto"/>
                            <DataGridTextColumn Header="NAME" Binding="{Binding Name}" Width="*"/>
                            <DataGridTextColumn Header="COMPANY" Binding="{Binding Company}" Width="*"/>
                            <DataGridTextColumn Header="SCORE" Binding="{Binding Score, StringFormat=N2}" Width="Auto"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
                <Grid>
                    <DataGrid ItemsSource="{Binding SessionOverall.Leaders}" Margin="50" BorderThickness="2">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="RANK" Binding="{Binding Rank}" Width="Auto"/>
                            <DataGridTextColumn Header="NAME" Binding="{Binding Name}" Width="*"/>
                            <DataGridTextColumn Header="COMPANY" Binding="{Binding Company}" Width="*"/>
                            <DataGridTextColumn Header="SCORE" Binding="{Binding Score, StringFormat=N2}" Width="Auto"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
            </ma:FlipView.Items>
        </ma:FlipView>
    </Grid>
</UserControl>

.xaml.cs:

public partial class Leaderboard : UserControl
{
    public Leaderboard()
    {
        InitializeComponent();
        var vm = new LeaderboardViewModel();
        vm.TextSize = TextSize;
        DataContext = vm;
    }

    #region Dependency Properties

    public int TextSize
    {
        get { return (int)GetValue(TextSizeProperty); }
        set { SetValue(TextSizeProperty, value); }
    }

    public static readonly DependencyProperty TextSizeProperty =
        DependencyProperty.Register("TextSize", typeof(int),
          typeof(Leaderboard));

    #endregion
}

视图模型:

public class LeaderboardViewModel : BindableBase
{
    public LeaderboardViewModel()
    {

    }

    #region Properties

    /// <summary>
    /// Font size
    /// </summary>
    private int textSize;
    public int TextSize
    {
        get { return textSize; }
        set { SetProperty(ref textSize, value); }
    }

    /// <summary>
    /// Show overall game score leaderboard
    /// </summary>
    private Leaderboard showOverall;
    public Leaderboard ShowOverall
    {
        get { return showOverall; }
        set { SetProperty(ref showOverall, value); }
    }

    /// <summary>
    /// Session overall game score leaderboard
    /// </summary>
    private Leaderboard sessionOverall;
    public Leaderboard SessionOverall
    {
        get { return sessionOverall; }
        set { SetProperty(ref sessionOverall, value); }
    }

    #endregion
}

每次我使用

设置TestSize属性
<Leaderboard TextSize="20"/>

并检查.xaml.cs,我的TextSize为0.我知道我做错了吗?

1 个答案:

答案 0 :(得分:1)

我假设绑定试图在TextSize中查找DataContext属性,该属性是viewmodel,而不是UserControl自己的属性。

尝试为UserControl

命名
<UserControl .... x:Name="leaderBoard">

并将绑定更改为以下内容:

<Setter Property="FontSize" Value="{Binding TextSize, ElementName=leaderBoard}"/>

如果绑定出现问题,请始终检查Visual Studio的“输出”窗格。应该在那里列出绑定错误,并给出一些可能出错的提示。