相关疑难解决方法(0)

在WPF中使用MVVM将n个矩形添加到画布

我想在我的mvvm应用程序的主窗口中添加一组矩形.在我的viewModel中,我有一个对象集合,我用转换器转换为System.Windows.Shapes.Rectangle类(代码如下):

视图模型:

RecognizedValueViewModel 
{
    public ObservableCollection<BarcodeElement> BarcodeElements
    {
        get { return _BarcodeElements; }
        set { _BarcodeElements = value; }
    }

    public RecognizedValueViewModel()
    {
        BarcodeElements = InitializeBarcodeElements();
    }
}
Run Code Online (Sandbox Code Playgroud)

转换器:

public BarcodeElementToRectangleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Rectangle barcodeRectangle = GetRectangleFromBarcodeElement(value as BarcodeElement);

        return barcodeRectangle;
    }
}
Run Code Online (Sandbox Code Playgroud)

矩形应显示在MainWindow的画布中:

<Canvas x:Name="Canvas_Image_Main">
    <!-- Show rectangles here -->
</Canvas>
Run Code Online (Sandbox Code Playgroud)

我会在代码中将Rectangles添加到canvas中,但我现在还没有在运行时有多少个矩形.我有办法实现这个目标吗?保护你.

c# wpf user-interface canvas mvvm

17
推荐指数
2
解决办法
2万
查看次数

样式设置器中的UWP绑定无法正常工作

我有创建xaml控件的问题.我正在使用通用应用程序在VS 2015中编写新项目.我想创建网格.在这个网格中我想要一个按钮.在模型中,我指定了列(Level)和Row.这是我的代码:

<ItemsControl Grid.Row="1" ItemsSource="{Binding Path=TechnologyList}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                    <RowDefinition Height="10*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                    <ColumnDefinition Width="14*"/>
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="Control">
            <Setter Property="Grid.Column" Value="{Binding Level}" />
            <Setter Property="Grid.Row" Value="{Binding Row}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding Name}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)

我遇到错误<Setter Property="Grid.Column" Value="{Binding Level}" /> 错误:HRESULT的异常:0x8000FFFF(E_UNEXPECTED)在edytor中没有运行代码.怎么了?在"旧"WPF中一切都很好,但在Universal …

c# setter xaml itemcontainerstyle win-universal-app

11
推荐指数
1
解决办法
7004
查看次数