0

好几个小时的谷歌搜索和 stakoverflow 阅读,我一直无法找到这个问题的答案。我有一个UserControl用于显示 aProgressBar类型的DependencyPropertya double

MainPage.XAML.cs 包含DataContext

void MainPage_Loaded(object sender,RoutedEventArgs e)
{
        setDataContext();
        MainGameListBox.ItemsSource = vm.GameList;
}

这是 MainPage.XAML 中的内容:

<ListBox Grid.Row="1" Grid.ColumnSpan="2" x:Name="MainGameListBox"
         SelectionChanged="listBoxGameSearch_SelectionChanged" >
    <!-- set its ItemsPanel to be a WrapPanel -->
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <toolkit1:WrapPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <toolkit1:ContextMenuService.ContextMenu>
                    <toolkit1:ContextMenu>
                        <toolkit1:MenuItem Header="Pin to start" Click="PinGameToStart_Click" />
                    </toolkit1:ContextMenu>
                </toolkit1:ContextMenuService.ContextMenu>
                <Grid Width="173" Height="173"
                      Background="{StaticResource PhoneAccentBrush}" Margin="12">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="32"/>                                    
                        <RowDefinition Height="32"/>
                        <RowDefinition Height="32"/>
                        <RowDefinition Height="32"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="86.5"/>
                        <ColumnDefinition Width="86.5"/>    
                    </Grid.ColumnDefinitions>
                    <Border Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" Width="64"
                            Height="64" BorderBrush="#70BC1F" BorderThickness="2"
                            Margin="6,6,0,0" VerticalAlignment="Top"
                            HorizontalAlignment="Left">
                        <Image  Source="{Binding GameTile,
                                Converter={StaticResource imageCacheConverter}}" />
                    </Border>
                    <view:CircularProgressChart x:Name="circularProgChart"
                                                Grid.Row="0" Grid.Column="1"
                                                Grid.RowSpan="3" Grid.ColumnSpan="2"
                                                Margin="6"
                                                Loaded="CircularProgressChart_Loaded"
                                                CompletionPercent="{Binding CompletionPercentage}" />
                </Grid>           
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

CompletionPercent是 DP,下面UserControl是:

public partial class CircularProgressChart:UserControl
{

    public double CompletionPercent
    {
        get { return (double)GetValue(CompletionPercentProperty); }
        set { SetValue(CompletionPercentProperty, value); }
    }

    public static readonly DependencyProperty CompletionPercentProperty = DependencyProperty.Register("CompletionPercent", typeof(double), typeof(CircularProgressChart), new PropertyMetadata(0.0, CompletionPercentChanged));

    public CircularProgressChart()
    {
        try
        {
            InitializeComponent();
            DataContext = this;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

这是CompletionPercentage物业:

public class Progress : INotifyPropertyChanged
{ 
    private double _completionPercentage = 0.0;
    public double CompletionPercentage
    {
        get{return _completionPercentage;}
        set{
            _completionPercentage = value;
            NotifyPropertyChanged("CompletionPercentage");
        }
    }
    protected void NotifyPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

问题,为什么 CompletionPercent="{Binding CompletionPercentage}" 不被束缚?它获取默认值 0,但是当CompletionPercentageDP 更新时,DP 不会得到更新。我已经检查了该NotifyPropertyChanged方法,它可以正确触发并在代码的所有其他部分中工作。

4

1 回答 1

0

原因是构造函数中的DataContext = thisCircularProgressChart{Binding CompletionPercentage}在 中查找CompletionPercentage属性DataContext,显然在 中不存在CircularProgressChart。类的实例Progress永远不会到达CircularProgressChart(依赖属性的显式分配优先于绑定)。

解决方案1(如果您真的想保留依赖属性):删除该DataContext = this;行。如果您需要绑定到CircularProgressChart其 XAML 中的属性,请指定正确的绑定源,而不是依赖DataContext.

解决方案 2(从 派生时很常见UserControl):完全删除CompletionPercent依赖属性(包括其绑定 in MainPage.xaml)并CompletionPercentage直接绑定到 in CircularProgressChart.xaml。同时删除DataContext = this;线。

解决方案 3(真正的 WPF 方式):将默认ProgressBar控件与自定义模板一起使用,而不是创建自己的控件。

于 2013-01-11T04:05:51.313 回答