使用 DataBinding 值更新控件的宽度 (WPF)

发布于 2024-07-27 16:21:17 字数 237 浏览 2 评论 0原文

我创建了一个用户控件和一个关联的视图模型。 视图模型的属性“DisplayName”和“TimeIntervalLength”显示在用户控件视图DataBinding中。 根据视图模型的这些属性,我想更新控件的宽度。 宽度应为“TimeIntervalLength”,但至少应为“DisplayName”。 我尝试覆盖“OnPropertyChanged”,但这不起作用。 此外,我找不到合适的事件来覆盖。

提前致谢。

I created a user control and an associated view model. The properties "DisplayName" and "TimeIntervalLength" of the view model are displayed in the user control view DataBinding. Depending on those properties of the view model I want to update the width of my control.
The width should be "TimeIntervalLength" but at least "DisplayName". I tried to override "OnPropertyChanged" but this does not work. Further I could not find an appropriated event to override.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

找回味觉 2024-08-03 16:21:17

您可以尝试不在用户控件上指定宽度/高度。 它应该适合其中托管的控件。

<UserControl x:Class="MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBlock x:Name="TextBlock1" Text="DisplayName Goes Here" />
        <local:TimeIntervalControl x:Name="TimeInterval" />
    </StackPanel>
</UserControl>

另一种选择是使用 IValueConverter 来做一些更繁重的工作:

<UserControl x:Class="MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <UserControl.Resources>
        <local:MaxValueConverter x:Key="MaxValue" />
    </UserControl.Resources>
    <UserControl.Width>
        <MultiBinding Converter="{StaticResource MaxValue}">
            <Binding Path="ActualWidth" ElementName="TextBlock1" />
            <Binding Path="ActualWidth" ElementName="TimeInterval" />
        </MultiBinding>
    </UserControl.Width>
    <StackPanel>
        <TextBlock x:Name="TextBlock1" Text="DisplayName Goes Here" />
        <local:TimeIntervalControl x:Name="TimeInterval" />
    </StackPanel>
</UserControl>

在 MaxValueConverter 中进行繁重的工作:

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
   if (values == null)
   {
       return Binding.DoNothing;
   }

   return values.Max(obj => (obj is double) ? (double)obj : 0.0);
}

You may try just not specifying a Width/Height on your UserControl. It should fit to the controls hosted within.

<UserControl x:Class="MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBlock x:Name="TextBlock1" Text="DisplayName Goes Here" />
        <local:TimeIntervalControl x:Name="TimeInterval" />
    </StackPanel>
</UserControl>

Another option is to use an IValueConverter to do some heavier lifting:

<UserControl x:Class="MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <UserControl.Resources>
        <local:MaxValueConverter x:Key="MaxValue" />
    </UserControl.Resources>
    <UserControl.Width>
        <MultiBinding Converter="{StaticResource MaxValue}">
            <Binding Path="ActualWidth" ElementName="TextBlock1" />
            <Binding Path="ActualWidth" ElementName="TimeInterval" />
        </MultiBinding>
    </UserControl.Width>
    <StackPanel>
        <TextBlock x:Name="TextBlock1" Text="DisplayName Goes Here" />
        <local:TimeIntervalControl x:Name="TimeInterval" />
    </StackPanel>
</UserControl>

Heavy lifting in your MaxValueConverter:

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
   if (values == null)
   {
       return Binding.DoNothing;
   }

   return values.Max(obj => (obj is double) ? (double)obj : 0.0);
}
徒留西风 2024-08-03 16:21:17

不完全确定我是否理解正确,但听起来最简单的方法是将计算属性添加到 ViewModel 中,然后从视图绑定到该属性:(

// In your model
public int DisplayWidth {
    get { return CalculateDisplayWidth(); } // todo
}

显然,您可以将“CalculateDisplayWidth()”替换为您需要的任何内容)

<!-- In your view -->
<UserControl x:Class="MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="{Binding Path=DisplayWidth, Mode=OneWay}">

</UserControl>

Not entirely sure if I understand correctly, but it sounds like the easiest thing to do would be to add a calculated property to your ViewModel and then bind to that from the View:

// In your model
public int DisplayWidth {
    get { return CalculateDisplayWidth(); } // todo
}

(Obviously you can replace "CalculateDisplayWidth()" with whatever you need)

<!-- In your view -->
<UserControl x:Class="MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="{Binding Path=DisplayWidth, Mode=OneWay}">

</UserControl>
岁月打碎记忆 2024-08-03 16:21:17

控制 MinWidth,而不是控制宽度。 并在 UserControl 的构造函数中清除 Width 属性:

this.ClearValue(WidthProperty);

Instead of controlling Width, control MinWidth. And clear the Width property in the constructor of UserControl:

this.ClearValue(WidthProperty);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文