프록시 컨트롤을 만들면 코드없이이 문제를 해결할 수 있습니다. 사람들은 종종이 접근법을 사용하여 this question에 대한 응답에서와 같이 바인딩 할 수있는 ActualWidth 및 ActualHeight 속성을 올바르게 업데이트합니다.
경우에 따라 프록시 컨트롤의 요소를 전체 창 크기로 늘어나는 일부 요소에 바인딩 할 수 있습니다. 바운드 요소의 너비 또는 높이가 변경되면 프록시 컨트롤의 두 속성 값을 계산 된 비례 높이 및 너비로 설정할 수 있습니다. 그런 다음 사용자 정의 컨트롤의 너비와 높이에 대해 이러한 비례 크기에 바인딩 할 수 있습니다. 예를 들어
: UserControl을 변경하여 그 다음 XAML에서
public class ProportionalSizeHelper : FrameworkElement, INotifyPropertyChanged
{
private double _proportionalWidth = 0;
private double _proportionalHeight = 0;
public event PropertyChangedEventHandler PropertyChanged;
public FrameworkElement Element
{
get { return (FrameworkElement)GetValue(ElementProperty); }
set { SetValue(ElementProperty, value); }
}
public double ProportionalHeight
{
get{ return _proportionalHeight; }
}
public double ProportionalWidth
{
get { return _proportionalWidth; }
}
public static readonly DependencyProperty ElementProperty =
DependencyProperty.Register("Element", typeof(FrameworkElement), typeof(ProportionalSizeHelper),
new PropertyMetadata(null,OnElementPropertyChanged));
private static void OnElementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ProportionalSizeHelper)d).OnElementChanged(e);
}
private void OnElementChanged(DependencyPropertyChangedEventArgs e)
{
FrameworkElement oldElement = (FrameworkElement)e.OldValue;
FrameworkElement newElement = (FrameworkElement)e.NewValue;
if (newElement != null)
{
newElement.SizeChanged += new SizeChangedEventHandler(Element_SizeChanged);
}
else
{
_proportionalHeight = 0;
_proportionalWidth = 0;
}
if (oldElement != null)
{
oldElement.SizeChanged -= new SizeChangedEventHandler(Element_SizeChanged);
}
NotifyPropChange();
}
private void Element_SizeChanged(object sender, SizeChangedEventArgs e)
{
// This can be whatever calculation you need
double factor = Math.Min(Element.ActualWidth/1024, Element.ActualHeight/768);
_proportionalHeight = 100*factor;
_proportionalWidth = 300*factor;
NotifyPropChange();
}
private void NotifyPropChange()
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("ProportionalWidth".NonNls()));
PropertyChanged(this, new PropertyChangedEventArgs("ProportionalHeight".NonNls()));
}
}
}
은 무엇이든 당신이 필요합니다
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<c:ProportionalSizeHelper Element="{Binding ElementName=LayoutRoot}" x:Name="proxy" />
</Grid.Resources>
<UserControl Width="{Binding ProportionalWidth, ElementName=proxy}" Height={Binding ProportionalHeight, ElementName=proxy} />
</Grid>
좋은 으론을 알려주세요? –
아래쪽의 대답을보세요 http://stackoverflow.com/questions/5082610/get-and-set-screen-resolution –
@meds, 잘 알고 있겠지만, 현재 창 크기가 무엇이든간에 제어 크기가 화면 해상도에 비례합니까? – dkozl