代码之家  ›  专栏  ›  技术社区  ›  R4cOOn

将文本块调整为动态更改宽度的按钮

  •  0
  • R4cOOn  · 技术社区  · 15 年前

    我有一个 TextBlock 在一个 Button 在一个 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

    我的问题是 纽扣 通过本地化资源动态设置,因此按钮的宽度会动态更改。

    适用于非动态的静态解决方案 纽扣 调整大小为:

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

    想法,任何人?我目前正在考虑将一个行为类粘贴到 控件 它倾听 SizeChanged 事件上 纽扣 . 我想有一个内置的解决方案,如果它存在的话。

    1 回复  |  直到 14 年前
        1
  •  1
  •   R4cOOn    15 年前

    如果有人感兴趣,下面是我在行为方面的表现。 我传递 Height Width 根据我想要绑定的属性。

    这是课程:

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

    希望能帮助别人。