мой параметр создал не работает usercontrol wpf - PullRequest
0 голосов
/ 01 июня 2018
    <Border Grid.Row="0"  Height="auto" x:Name="BorderEcs" Background="#9494a5"   BorderThickness="1,0,1,1" BorderBrush="#9494a5"  CornerRadius="10,10,0,0">
        <StackPanel Height="auto">
            <Label x:Name="LblTitl" Content="" Margin="5,0,0,0" Height="auto"  Foreground="#FFFFFF" FontFamily="Century Gothic" FontSize="13" />
            <DataGrid  Width="{Binding ControlWidth, RelativeSource={RelativeSource Self},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                       Height="{Binding ControlHeight, RelativeSource={RelativeSource Self},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"           
                           CellStyle="{StaticResource DataGridContentCellCentering}"
                           RowStyle="{StaticResource RowStyle}" 
                           ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}" 
                           Style="{StaticResource DataGridStyle}"  
                           AlternatingRowBackground="White" 
                           AlternationCount="2">
            </DataGrid>
        </StackPanel>

{открытый частичный класс UCDataGrid: UserControl, INotifyPropertyChanged {public UCDataGrid () {InitializeComponent ();DataContext = это;} public static DependencyProperty ControlHeightProperty = DependencyProperty.Register ("ControlHeight", typeof (int), typeof (UCDataGrid));

    public static  DependencyProperty ControlWidthProperty =
        DependencyProperty.Register("ControlWidth", typeof(int), typeof(UCDataGrid));

    public event PropertyChangedEventHandler PropertyChanged;

    public int ControlHeight
    {
        get { return (int)GetValue(ControlHeightProperty); }
        set { SetValue(ControlHeightProperty, value);
            OnProperyChanged("ControlHeight");
        }
    }

    public int ControlWidth
    {
        get { return (int)GetValue(ControlWidthProperty); }
        set { SetValue(ControlWidthProperty, value); OnProperyChanged("ControlWidth"); }
    }
    public void OnProperyChanged(string propName = null)
    {
        PropertyChangedEventHandler propertyChanged = PropertyChanged;
        if (propertyChanged != null)
        {
            propertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

}

    <us:UCDataGrid x:Name="d" ControlWidth="1" ControlHeight="10" />

1 Ответ

0 голосов
/ 01 июня 2018

Пожалуйста, попробуйте ниже код, он работал для меня.Для простоты я только что включил код для ControlWidth

public partial class UCDataGrid : UserControl
{
    public UCDataGrid()
    {
        InitializeComponent();
    }

    public static  DependencyProperty ControlWidthProperty =
    DependencyProperty.Register("ControlWidth", typeof(double), typeof(UCDataGrid),new UIPropertyMetadata());

    public double ControlWidth
    {
       get { return (double)GetValue(ControlWidthProperty); }
       set { SetValue(ControlWidthProperty, value);  }
    }
}

Теперь установите Name в UserControl в xaml и получите доступ к нему, чтобы установить привязку.В настоящее время удалите все стили и убедитесь, что ширина и высота не установлены в любом другом месте.

<UserControl Name="root">
 <Border Grid.Row="0"  Height="auto" x:Name="BorderEcs" Background="#9494a5"   BorderThickness="1,0,1,1" BorderBrush="#9494a5"  CornerRadius="10,10,0,0">
    <StackPanel Height="auto">
        <Label x:Name="LblTitl" Content="" Margin="5,0,0,0" Height="auto"  Foreground="#FFFFFF" FontFamily="Century Gothic" FontSize="13" />
        <DataGrid  Width="{Binding ElementName=root, Path=ControlWidth, UpdateSourceTrigger=PropertyChanged}" 
                   Height="{Binding ElementName=root, Path=ControlHeight, UpdateSourceTrigger=PropertyChanged}"           
                       CellStyle="{StaticResource DataGridContentCellCentering}"
                       RowStyle="{StaticResource RowStyle}" 
                       ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}" 
                       Style="{StaticResource DataGridStyle}"  
                       AlternatingRowBackground="White" 
                       AlternationCount="2">
        </DataGrid>
    </StackPanel>
    </UserControl>

Надеюсь, это поможет

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...