我有一个主窗口viewmodel,它正在创建usercontrol的实例,并在特定功能中通过代码(即
)设置usercontrol的属性。 // Set Button Width Height
var potentialHeight = (SelectorUCViewModel.GridHeight /
SelectorUCViewModel.ColumnCount) - (SelectorUCViewModel.ButtonMarginToUse * 2);
var potentialWidth = (SelectorUCViewModel.GridWidth /
SelectorUCViewModel.RowCount) - (SelectorUCViewModel.ButtonMarginToUse * 2);
SelectorUCViewModel.ButtonHeightWidth = (potentialHeight < potentialWidth) ? potentialHeight : potentialWidth;
调用此选项时,用户控件中的属性会更改(您可以看到setter被调用了)。但是,在UI上似乎什么也没发生(没有边距...没有切换按钮...)
我不明白为什么。...
我的视图模型的XAML摘录具有以下内容:
<DockPanel LastChildFill="False">
<views:SelectorUC x:Name="UC" DataContext="{Binding SelectorUCViewModel}" />
<Border DockPanel.Dock="Right" Width="160" Margin="10 0 10 0">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Center">
然后UserControl具有以下XAML
<UserControl x:Class="SelectorUC"
x:Name="UC"
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:Selector"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Name="mainGrid" DockPanel.Dock="Left" MouseDown="MainGrid_MouseDown" MouseUp="MainGrid_MouseUp" MouseMove="MainGrid_MouseMove" Background="Transparent"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
MinWidth="{Binding GridWidth}" Height="{Binding GridHeight}">
<ItemsControl x:Name="objItemControl" ItemsSource="{Binding ObjCompositeCollection}">
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Row" Value="{Binding Row}"/>
<Setter Property="Grid.Column" Value="{Binding Column}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid DockPanel.Dock="Top" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Name="grid" Grid.Row="1"
Width="{Binding MinWidth, ElementName=mainGrid}"
Height="{Binding Height, ElementName=mainGrid}"
GridHelper.RowCount="{Binding RowCount}"
GridHelper.ColumnCount="{Binding ColumnCount}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type ObjA}">
<ToggleButton Content="{Binding Id}"
Tag="{Binding Id}"
IsChecked="{Binding IsSelected}"
Height="{Binding Path=ButtonHeightWidth, ElementName=UC}"
Width="{Binding Path=ButtonHeightWidth, ElementName=UC}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Margin="{Binding Path=ButtonMarginToUse, ElementName=UC}"
Padding="2"
PreviewMouseLeftButtonDown="Button_PreviewMouseLeftButtonDown"
PreviewMouseLeftButtonUp="Button_PreviewMouseLeftButtonUp"
MouseEnter="Button_MouseEnter"
MouseLeave="Button_MouseLeave"
Style="{StaticResource ObjButton}">
</ToggleButton>
</DataTemplate>
<DataTemplate DataType="{x:Type GridLabeller}">
<TextBlock Text="{Binding HeaderName}" Style="{StaticResource GridHeaders}"/>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
<Canvas>
<Rectangle x:Name="selectionBox" Visibility="Collapsed" Stroke="{StaticResource ResourceKey=SecondaryThemeColour}" StrokeThickness="2" StrokeDashArray="2,1"/>
</Canvas>
</Grid>
</UserControl>
以及正在UC类中重置的属性
private int buttonHeightWidth;
public int ButtonHeightWidth
{
get { return buttonHeightWidth; }
set
{
buttonHeightWidth = value;
OnPropertyChanged(nameof(ButtonHeightWidth));
}
}
private int buttonMarginToUse = 2;
public int ButtonMarginToUse
{
get { return buttonMarginToUse; }
set
{
buttonMarginToUse = value;
OnPropertyChanged(nameof(ButtonMarginToUse));
}
}
mainwindow视图设置mainwindowview模型的数据上下文
public MainWindow()
{
InitializeComponent();
viewModel = new MainWindowViewModel(model);
DataContext = viewModel;
SourceInitialized += Window1_SourceInitialized;
}
我真的无法理解什么是错的-没有绑定错误,但是由于将我的代码从viewmodel转移到usercontrol(试图从vm尽可能多地抽象出来),所以一切都无法正常工作... < / p>