更新Canvas以及Databound属性

时间:2015-01-12 20:19:33

标签: c# wpf xaml canvas

我创建了一个类" MyPolygonClass"我从中创建了一个我将数据绑定到用户控件的实例。

这很好用。

但是,如果用户更改了我想要更新画布的多边形边数,也显示在用户控件中 - 但不是MyPolygonClass的一部分

我最初运行UpdateCanvas(),但如果_poly更新,则不会更新。每次更改_poly时如何调用UpdateCanvas()

public partial class MainWindow : Window
{
    private MyPolygonClass _poly;

    public MainWindow()
    {
        InitializeComponent();
    }

    public void UpdateCanvas()
    {            
        //Create a canvas
        Canvas drawingCanvas = GenerateCanvas(this._poly.CornerCount);

        //Overwrite the existing Canvas
        var vb = drawingPlaceholder.Parent as Viewbox;
        if (vb != null)
        {
            vb.Child = drawingCanvas;
        }
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {            
        BackgroundWorker worker = new BackgroundWorker();
        worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        worker.DoWork += worker_DoWork;
        worker.RunWorkerAsync();
    }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        //Gets the Info from a Database
        this._poly = DataSupplier.GetPolygon();              
    }

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //Do Databinding Here
        Menu.DataContext = this._poly;            

        // Display
        UpdateCanvas();
    }
}

XAML:

<Grid.Resources>
<DataTemplate x:Key="LegTemplate">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="5"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="2*"></ColumnDefinition>
            <ColumnDefinition Width="2*"></ColumnDefinition>
            <ColumnDefinition Width="3*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Viewbox Grid.Row="0" Grid.Column="0"  >
            <Label Name="IdLabel" Content="{Binding Path=Name}"/>
        </Viewbox>

        <WrapPanel Grid.Row="0" Grid.Column="3">                           
            <Button FontFamily="Marlett" Name="ButtonUp" Content="5" Click="Button_Click_Move_Leg_Up"  Style="{StaticResource LegButtonUp}"></Button>
            <Button FontFamily="Marlett" Name="ButtonDown" Content="6" Click="Button_Click_Move_Leg_Down"  Style="{StaticResource LegButtonDown}"></Button>
            <Button Name="ButtonDelete" Content="x" Click="Button_Click_Delete_Leg" Style="{StaticResource LegButtonDel}"></Button>
        </WrapPanel>

        <TextBox Grid.Row="0" Grid.Column="1" Name="TextBoxLength" Text="{Binding Path=Length, Converter={StaticResource DoubleConverter}, ConverterCulture=de-de, Mode=TwoWay,  StringFormat=N1}" TextAlignment="Right"/>
        <TextBox Grid.Row="0" Grid.Column="2" Name="TextBoxAngle" Text="{Binding Path=Angle, Converter={StaticResource DoubleConverter}, ConverterCulture=de-de, Mode=TwoWay,  StringFormat=N1}" TextAlignment="Right"/>

    </Grid>
</DataTemplate>

XAML2:

<StackPanel>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition Height="5"></RowDefinition>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"></ColumnDefinition>
        <ColumnDefinition Width="2*"></ColumnDefinition>
        <ColumnDefinition Width="2*"></ColumnDefinition>
        <ColumnDefinition Width="3*"></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <Label Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right">Length</Label>
    <Label Grid.Row="0" Grid.Column="2" HorizontalAlignment="Right">Angle</Label>
</Grid>

<ItemsControl x:Name="LbLegs" ItemTemplate="{DynamicResource LegTemplate}" />

<Button Content="AddLeg" Click="Button_Click_2"></Button>

0 个答案:

没有答案