将TextBlock的大小调整为动态更改宽度的Button

时间:2009-11-18 11:06:08

标签: silverlight xaml

我的TextBlock位于Button的{​​{1}}之上。我想这样显示:

Grid

但是,我得到的是:

"some very long text" <-- text
"that doesn't fit" <-- text wrapped
[my button text size] <-- button

我的问题是 "some very long text that doesn't fit" <-- text
[my button text size] <-- button
中的文本是通过本地化资源动态设置的,因此按钮的宽度会动态变化。

适用于非动态Button调整大小的静态解决方案是:

Button

想法,有人吗?我目前正在考虑将一个Behavior类添加到 <TextBlock Margin="5" TextWrapping="Wrap" Width="{Binding ElementName=requestDemoButton, Path=RenderSize.Width}" Text="{Binding Path=Resource.Text, Source={StaticResource LocalizedStrings }}" /> <Button x:Name="requestDemoButton" Margin="5" Height="Auto" Width="Auto" HorizontalAlignment="Right" Content="{Binding Path=Resource.Button, Source={StaticResource LocalizedStrings }}" /> 来监听TextBlock上的SizeChanged事件。如果它存在,我想要一个内置的解决方案。

1 个答案:

答案 0 :(得分:1)

如果有人感兴趣,这就是我在行为中的表现。 我根据我想要绑定的属性传递HeightWidth

这是班级:

public static class DynamicControlResizeBehavior
{
    public static readonly DependencyProperty TargetProperty =
        DependencyProperty.RegisterAttached("Target", typeof(FrameworkElement), typeof(DynamicControlResizeBehavior), new PropertyMetadata(OnTargetSetCallback));

    public static readonly DependencyProperty PropertyNameProperty =
        DependencyProperty.RegisterAttached("PropertyName", typeof(string), typeof(DynamicControlResizeBehavior), new PropertyMetadata(OnPropertyNameSetCallback));


    public static string GetPropertyName(DependencyObject obj)
    {
        return (string)obj.GetValue(PropertyNameProperty);
    }

    public static void SetPropertyName(DependencyObject obj, string value)
    {
        obj.SetValue(PropertyNameProperty, value);
    }

    public static FrameworkElement GetTarget(DependencyObject obj)
    {
        return (FrameworkElement)obj.GetValue(TargetProperty);
    }

    public static void SetTarget(DependencyObject obj, FrameworkElement value)
    {
        obj.SetValue(TargetProperty, value);
    }

    private static void SynchronizeProperty(DependencyObject dependencyObject)
    {
        var target = GetTarget(dependencyObject);
        if (target != null)
        {
            var propertyName = GetPropertyName(dependencyObject);

            DependencyProperty dependencyToRead;
            DependencyProperty dependencyToWrite;
            if (string.Equals(propertyName, "Width", StringComparison.InvariantCulture))
            {
                dependencyToRead = FrameworkElement.ActualWidthProperty;
                dependencyToWrite = FrameworkElement.WidthProperty;
            }
            else if (string.Equals(propertyName, "Height", StringComparison.InvariantCulture))
            {
                dependencyToRead = FrameworkElement.ActualHeightProperty;
                dependencyToWrite = FrameworkElement.HeightProperty;
            }
            else
            {
                return;
            }

            var propertySize = (double)target.GetValue(dependencyToRead);
            dependencyObject.SetValue(dependencyToWrite, propertySize);
        }
    }

    private static void OnTargetSetCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var oldElement = e.OldValue as FrameworkElement;
        if (oldElement != null)
        {
            oldElement.SizeChanged -= (o, s) => SynchronizeProperty(d);
        }

        var newElement = e.NewValue as FrameworkElement;
        if (newElement != null)
        {
            newElement.SizeChanged += (o, s) => SynchronizeProperty(d);
        }
    }

    private static void OnPropertyNameSetCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        SynchronizeProperty(d);
    }
}

以下是它的使用方法:

            <TextBlock
                Behaviors:DynamicControlResizeBehavior.Target="{Binding ElementName=submitButton}"
                Behaviors:DynamicControlResizeBehavior.PropertyName="Width"
                HorizontalAlignment="Right"
                Margin="5,20,5,5"
                TextWrapping="Wrap"
                Text="{Binding Path=Resource.RequestDemoLoginText, Source={StaticResource LocalizedStrings }}"
                />
            <Button 
                x:Name="submitButton"
                Margin="5" 
                Height="Auto" 
                Width="Auto" 
                HorizontalAlignment="Right"
                HorizontalContentAlignment="Left"
                Content="{Binding Path=Resource.RequestDemoLogin, Source={StaticResource LocalizedStrings }}" />

希望这可能有助于其他人。