C# 如何通过UserControl参数管理控件的宽度?

C# 如何通过UserControl参数管理控件的宽度?,c#,.net,wpf,C#,.net,Wpf,简而言之,目标是将LabelWidth传播到我的UserControl类PropertyView中的子级。请参见此片段: <TabItem.Header>Press</TabItem.Header> <TabItem.DataContext> <Binding XPath="press_information"/> </TabItem.DataContext> <W3V:Propert

简而言之,目标是将LabelWidth传播到我的UserControl类PropertyView中的子级。请参见此片段:

     <TabItem.Header>Press</TabItem.Header>
    <TabItem.DataContext>
      <Binding XPath="press_information"/>
    </TabItem.DataContext>
    <W3V:PropertyView LabelWidth="200"></W3V:PropertyView>
在XAML中,使用以下绑定语法:

      <W3V:SimpleControl x:Name="simple"  Content="{Binding}" 
            LabelWidth="{Binding LabelWidth,
                         RelativeSource={RelativeSource AncestorType=W3V:PropertyView}}" />

我已经广泛地搜索了一个解决方案来处理这种情况的组合,尝试了许多没有成功的事情(简单的情况下成功了,但不是这个),我在这里不知所措。

您的静态资源绑定失败了吗?您是否在参考资料部分定义了值?尝试类似的方法(注意,我在这里直接写了这篇文章(不是在VS中),它应该非常正确:)


100

我会问您是否真的打算转到StaticResources,或者您是否打算绑定到viewmodel上的属性或视图的代码隐藏中的属性?

回答已编辑的问题

这是另一个基于精炼问题的问题。我仍然不能100%确定你想要实现什么,但至少以下几点可以为你指明正确的方向

所以有一个窗口只包含UserControl。此用户控件绑定到XML文件,它有三个标签字段。其中一个显示节点属性和以下XML文件内容。第一个标签的宽度绑定到MainWindow中的属性,另一个绑定到UserControl中的静态转换器资源

主窗口XAML

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:user="clr-namespace:WpfApplication"
        Title="MainWindow" Height="350" Width="600">
    <Grid>
        <user:XmlUserControl />
    </Grid>
</Window>
<UserControl x:Class="WpfApplication.XmlUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication2"
             mc:Ignorable="d" 
             d:DesignHeight="350" d:DesignWidth="350">

    <StackPanel>
        <StackPanel.Resources>
            <XmlDataProvider x:Key="XmlData" XPath="Data/Items" Source="Items.xml" />
            <local:NodeWidthConverter x:Key="NodeWidthConverter" />
        </StackPanel.Resources>

    <ItemsControl>
        <ItemsControl.ItemsSource>
            <Binding Source="{StaticResource XmlData}" XPath="*"/>
        </ItemsControl.ItemsSource>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Label x:Name="Title" VerticalAlignment="Bottom" 
                           FontWeight="Bold" HorizontalAlignment="Left"
                           Content="{Binding XPath=@Title}" />
                        <Label Background="BurlyWood" HorizontalAlignment="Left" 
                               Content="{Binding}" Width="{Binding ControlWidth}" />
                        <Label Background="BlanchedAlmond" HorizontalAlignment="Left"
                               Content="{Binding}" Width="{Binding ElementName=Title, Converter={StaticResource NodeWidthConverter}}"/>
                    </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    </StackPanel>
</UserControl>
XmlUserControl XAML

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:user="clr-namespace:WpfApplication"
        Title="MainWindow" Height="350" Width="600">
    <Grid>
        <user:XmlUserControl />
    </Grid>
</Window>
<UserControl x:Class="WpfApplication.XmlUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication2"
             mc:Ignorable="d" 
             d:DesignHeight="350" d:DesignWidth="350">

    <StackPanel>
        <StackPanel.Resources>
            <XmlDataProvider x:Key="XmlData" XPath="Data/Items" Source="Items.xml" />
            <local:NodeWidthConverter x:Key="NodeWidthConverter" />
        </StackPanel.Resources>

    <ItemsControl>
        <ItemsControl.ItemsSource>
            <Binding Source="{StaticResource XmlData}" XPath="*"/>
        </ItemsControl.ItemsSource>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Label x:Name="Title" VerticalAlignment="Bottom" 
                           FontWeight="Bold" HorizontalAlignment="Left"
                           Content="{Binding XPath=@Title}" />
                        <Label Background="BurlyWood" HorizontalAlignment="Left" 
                               Content="{Binding}" Width="{Binding ControlWidth}" />
                        <Label Background="BlanchedAlmond" HorizontalAlignment="Left"
                               Content="{Binding}" Width="{Binding ElementName=Title, Converter={StaticResource NodeWidthConverter}}"/>
                    </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    </StackPanel>
</UserControl>
NodeWidthConverter

using System;
using System.Globalization;
using System.Windows.Data;

namespace WpfApplication2
{
    public class NodeWidthConverter : IValueConverter 
    {
        public static Random Random = new Random();

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Random.Next(200, 600);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
Items.xml示例数据

  <Data>
    <Items>
      <Item Title="Title 1">
        <Name>This is item name 1</Name>
        <Summary>Summary for item 1</Summary>
      </Item>
      <Item Title="Title 2">
        <Name>This is item name 2</Name>
        <Summary>Summary for item 2</Summary>
      </Item>
      <Item Title="Title 3">
        <Name>This is item name 3</Name>
        <Summary>Summary for item 3</Summary>
      </Item>
    </Items>
  </Data>

这是项目名称1
项目1的摘要
这是项目名称2
项目2摘要
这是项目名称3
项目3的摘要
有了这个,你会得到以下结果。标签具有彩色背景,以可视化不断变化的宽度特性


希望这有帮助

那么,您只需要将
SimpleControl.LabelWidth
绑定到
PropertyView.LabelWidth
?可以通过以下方式实现:

<W3V:SimpleControl
    LabelWidth="{Binding Path=LabelWidth,
                     RelativeSource={RelativeSource AncestorType=PropertyView}}"

我没有资源。我有代码隐藏属性。也许我应该使用{Binding}而不是{StaticResource},但我试过了,结果同样失败。(可能还有一个datacontext会把事情搞砸?)或者我应该改为绑定一个UserControl.Resource并从那里继续吗?@AlanBaljeu你不想将这些值存储在StaticResources中,这就是我质疑它的原因。什么被设置为视图的DataContext?DataContext是一个XmlDocument,我正在显示它的一部分。如果我理解这一点,它可以工作,因为UserControl1的DataContext是它的父窗口MainWindow。但在我的例子中,DataContext是{StaticResource aa},它是一个XmlDocument。我想我需要覆盖绑定查找才能使其对我起作用。您需要绑定到更改的内容。如果上下文是XmlDocument,那么就不可能了。我仍然需要以某种方式设置宽度。然后通过另一种方法?@AlanBaljeu Widths of…?my usercontrol生成的控件的宽度。最终,我们要处理一个ItemTemplate来生成多对标签和内容,这些标签和内容必须是来自主窗口的参数化宽度。还可以复制/粘贴XML文件的结构和内容的简短示例吗?我编辑了我的答案,希望对您有所帮助。失败:PresentationFramework.dll中出现了“System.Windows.Markup.XamlParseException”类型的第一次异常。其他信息:无法在“SimpleControl”类型的“LabelWidth”属性上设置“Binding”。“绑定”只能在DependencyObject的DependencyProperty上设置。@AlanBaljeu是
SimpleControl
class您的吗
LabelWidth
属性应更改为dependency属性。WPF控件应该使用依赖属性,因为绑定和其他特性依赖于它。
using System.Windows.Controls;

namespace WpfApplication
{
    public partial class XmlUserControl : UserControl
    {
        public XmlUserControl()
        {
            InitializeComponent();
        }
    }
}
using System;
using System.Globalization;
using System.Windows.Data;

namespace WpfApplication2
{
    public class NodeWidthConverter : IValueConverter 
    {
        public static Random Random = new Random();

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Random.Next(200, 600);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
  <Data>
    <Items>
      <Item Title="Title 1">
        <Name>This is item name 1</Name>
        <Summary>Summary for item 1</Summary>
      </Item>
      <Item Title="Title 2">
        <Name>This is item name 2</Name>
        <Summary>Summary for item 2</Summary>
      </Item>
      <Item Title="Title 3">
        <Name>This is item name 3</Name>
        <Summary>Summary for item 3</Summary>
      </Item>
    </Items>
  </Data>
<W3V:SimpleControl
    LabelWidth="{Binding Path=LabelWidth,
                     RelativeSource={RelativeSource AncestorType=PropertyView}}"