开发者

Why my binding does not update on UIElement.RenderSize or UIElement.ActualWidth? [duplicate]

This question already has answers here: Binding to ActualWidth does not work (8 answers) Closed 3 years ago.

I'm trying to data bind a TextBlock to the RenderSize of a Rectangle. Here is the code.

<StackPanel x:Name="root">
        <Rectangle x:Name="rect" Fill="Green" RenderTransfor开发者_运维百科mOrigin="0.5,0.5" Height="100" Width="100" />

        <TextBlock Text="{Binding ElementName=rect, Path=Width}"></TextBlock>
        <TextBlock Text="{Binding ElementName=rect, Path=ActualWidth}"></TextBlock>
        <TextBlock Text="{Binding ElementName=rect, Path=RenderSize}"></TextBlock>
        <Slider Value="{Binding ElementName=rect, Path=Width, Mode=TwoWay}" Maximum="200"></Slider>
        <Button Content="Manually Get RenderSize" Click="Button_Click_1"></Button>
        <TextBlock x:Name="info"></TextBlock>
    </StackPanel>

When I move the slider, rect become larger, and the first TextBox is updated correctly. However, ActualWidth and RenderSize stay to 0, and 0,0.

When I click on Button, I get the RenderSize programmatically and show it in the last TextBlock, which is different than 0,0.

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    info.Text = rect.RenderSize.ToString();
}

So my binding on the RenderSize does not update the TextBox correctly... Why ? Thanks for your help,


RenderSize is not dependency property, but it corresponds to dependency properties (ActualWidth, ActualHeight)

So to bind to RenderSize we can use MultiBinding to (ActualWidth, ActualHeight)

<TextBlock>
  <TextBlock.Text>
    <MultiBinding Converter="{x:Static MyConv:SizePartsToSizeConverter.Instance}">
                    <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
                    <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

public class SizePartsToSizeConverter:IMultiValueConverter
{
    private static SizePartsToSizeConverter m_instance = new SizePartsToSizeConverter();
    public static SizePartsToSizeConverter Instance { get { return m_instance; } }

    public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
    {
        var values = value.Where(x => x is Double).Cast<double>().ToList();
        if (values.Count()!=2) return DependencyProperty.UnsetValue;

        return new Size(values[0],values[1]);
    }

    public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜