0

我有两个用户控件说“Usercntrl1”“Usercntrl2”,“Usercntrl1”包含Slider,“Usercntr2”有带图像的 ListBox,现在当我更改“Usercntrl1”中的Slider 值时, ListboxItems 大小(即)宽度和高度有随 Respect to Slider 值更改。当它们都出现在同一个用户控件中时,可以轻松完成,如下所示

<ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                        <Setter Property="Padding" Value="0"/>
                        <Setter Property="Margin" Value="1"/>
                        <Setter Property="BorderBrush" Value="Green"/>
                        <Setter Property="Width" Value="{Binding Path=Value, ElementName=sizeSlider, Mode=TwoWay}"/>
                        <Setter Property="Height" Value="{Binding Path=Value, ElementName=sizeSlider, Mode=TwoWay}"/>
                         <Setter Property="TabIndex" Value="1"/>
                                           </Style>
                </ListBox.ItemContainerStyle>

有人可以指导吗?

4

2 回答 2

1

如果你真的需要从一个用户控件绑定到另一个用户控件的属性,也许你可以使用这样的东西:

用户cntrl1:Xaml:

<UserControl x:Class="ListBoxSliderSizeTest.Usercntrl1"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" x:Name="MainControl">
   <Grid>
      <Slider Maximum="200" Minimum="1" Value="{Binding ElementName=MainControl, Path=SliderSize, Mode=TwoWay}"/>   
   </Grid>

代码隐藏:

...
public double SliderSize
  {
     get { return (double)GetValue(SliderSizeProperty); }
     set { SetValue(SliderSizeProperty, value); }
  }

  public static readonly DependencyProperty SliderSizeProperty =
      DependencyProperty.Register("SliderSize", typeof(double), typeof(Usercntrl1), new PropertyMetadata(0.0));
...

UserCntrl2:XAML:

<UserControl x:Class="ListBoxSliderSizeTest.Usercntrl2"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" x:Name="MainControl">
<Grid>
        <ListBox>
            <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
                <Setter Property="Padding" Value="0"/>
                <Setter Property="Margin" Value="1"/>
                <Setter Property="BorderBrush" Value="Green"/>
                <Setter Property="Width" Value="{Binding Path=SliderSize, ElementName=MainControl, Mode=OneWay}"/>
                <Setter Property="Height" Value="{Binding Path=SliderSize, ElementName=MainControl, Mode=OneWay}"/>
                <Setter Property="TabIndex" Value="1"/>
            </Style>

        </ListBox.ItemContainerStyle>
            <Button/>
        </ListBox>
</Grid>

代码隐藏:

---
      public double SliderSize
  {
     get { return (double)GetValue(SliderSizeProperty); }
     set { SetValue(SliderSizeProperty, value); }
  }

  public static readonly DependencyProperty SliderSizeProperty =
      DependencyProperty.Register("SliderSize", typeof(double), typeof(Usercntrl2), new PropertyMetadata(0.0));
...

然后使用这两个用户控件:

<listBoxSliderSizeTest:Usercntrl1 x:Name="Usercntrl1"/>
    <listBoxSliderSizeTest:Usercntrl2 Grid.Column="1" SliderSize="{Binding ElementName=Usercntrl1, Path=SliderSize}"/>

编辑:

所以如果 Usercntrl1 托管 UserControl2 它看起来像这样:

用户cntrl1:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Slider Maximum="200" Minimum="1" Value="{Binding ElementName=MainControl, Path=SliderSize, Mode=TwoWay}" x:Name="Slider"/>
    <listBoxSliderSizeTest:Usercntrl2 Grid.Row="1" SliderSize="{Binding ElementName=MainControl, Path=SliderSize}" />
</Grid>

或者:

    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Slider Maximum="200" Minimum="1" x:Name="Slider"/>
    <listBoxSliderSizeTest:Usercntrl2 Grid.Row="1" SliderSize="{Binding ElementName=Slider, Path=Value}" />
</Grid>

编辑2:

用户控件 1 XAML:

<Slider Maximum="200" Minimum="1" x:Name="Slider" Value="{Binding ElementName=MainControl, Path=SliderSize, Mode=TwoWay}" />
<TabControl x:Name="tc" Grid.Row="1"/>

后面的代码:

var uc = new Usercntrl2();
     var binding = new Binding("SliderSize") { Source = this,Mode = BindingMode.TwoWay};
     uc.SetBinding(Usercntrl2.SliderSizeProperty, binding);
     tc.Items.Add(uc);
于 2013-07-10T12:35:23.820 回答
0

对于您上次评论中的问题(仅将大小应用于活动项目):

使用触发器:

 <Style.Triggers>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True">
       <Setter Property="Width" Value="{Binding Path=SliderSize, ElementName=MainControl, Mode=OneWay}"/>
       <Setter Property="Height" Value="{Binding Path=SliderSize, ElementName=MainControl, Mode=OneWay}"/>
    </DataTrigger>
 </Style.Triggers>

还是要将其应用于活动选项卡中的所有项目?

于 2013-07-12T09:32:01.093 回答