WPF将c#类绑定到静态资源

时间:2017-02-22 11:19:15

标签: c# .net wpf xaml

我有一个xaml工具栏:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:logic="clr-namespace:DD.Logic;assembly=DD.Logic">

<ToolBar x:Key="MyToolbar" Height="120">
    <!--File-->
    <GroupBox Header="File" Style="{StaticResource ToolbarGroup}" Margin="5,3,3,3">
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Margin="3" Width="55" Style="{StaticResource ToolBarButtonBaseStyle}"
                    Command="{x:Static ApplicationCommands.New}">
                <Button.Content>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="4*"/>
                            <RowDefinition Height="1*"/>
                        </Grid.RowDefinitions>
                        <Image Source="Images/GenericDocument.png" Width="45"/>
                        <TextBlock Grid.Row="1" Text="New" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
                    </Grid>
                </Button.Content>
            </Button>
            <StackPanel Orientation="Vertical" Margin="0,2,0,2">
                <Button Margin="1" Padding="2" HorizontalContentAlignment="Left"
                        Style="{StaticResource ToolBarButtonBaseStyle}"
                        Command="{x:Static ApplicationCommands.Open}">
                    <Button.Content>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Image Source="Images/OpenFolder.png" Width="16"/>
                            <TextBlock Margin="3,0,3,0" Text="Open" VerticalAlignment="Center"  Grid.Column="1"/>
                        </Grid>
                    </Button.Content>
                </Button>
                <Button Margin="1" Padding="2" HorizontalContentAlignment="Left"
                        Style="{StaticResource ToolBarButtonBaseStyle}"
                        Command="{x:Static ApplicationCommands.Save}">
                    <Button.Content>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <Image Source="Images/Save.png" Width="16"/>
                            <TextBlock Margin="3,0,3,0" Text="Save" VerticalAlignment="Center" Grid.Column="1"/>
                        </Grid>
                    </Button.Content>
                </Button>
                <Button Margin="1" Padding="2" HorizontalContentAlignment="Left"
                        Style="{StaticResource ToolBarButtonBaseStyle}"
                        Command="{x:Static ApplicationCommands.Print}">
                    <Button.Content>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>
                            <Image Source="Images/Print.png" Width="16"/>
                            <TextBlock Margin="3,0,3,0" Text="Print" VerticalAlignment="Center" Grid.Column="1"/>
                        </Grid>
                    </Button.Content>
                </Button>
            </StackPanel>
        </StackPanel>
    </GroupBox>

    <!--Format-->
    <GroupBox Header="Format" Style="{StaticResource ToolbarGroup}" Margin="3">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="0">Text font size:</TextBlock>
                <ComboBox VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="1"
                Height="20"
                Width="80"
                IsEditable="True"
                Loaded="{x:Static logic:ApplicationToolbar.RoutedEvent}"
                TextBoxBase.TextChanged="{x:Static logic:ApplicationToolbar.SelectionChangedEvent}"
            />
            </Grid>
            <Grid Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="0">Arrows:</TextBlock>
                <ComboBox VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="1"
                          Name ="ArrowsCombobox"
                Height="20"
                Width="80"
                Loaded="{x:Static logic:ApplicationToolbar.ArrowsComboboxLoaded}"
                SelectionChanged="{x:Static logic:ApplicationToolbar.ArrowsComboboxSelectionChanged}"
            />
            </Grid>
        </Grid>
    </GroupBox>
</ToolBar>

在主窗口中显示为:

<ContentControl  Content="{StaticResource MyToolbar}"/>

实现相应命令和偶数代码的类:

public class ApplicationToolbar : Control
{
    public static PathFinderType PathFinderType = PathFinderType.Straight;

    public static RoutedEventHandler RoutedEvent = LoadFontSizes;
    public static TextChangedEventHandler SelectionChangedEvent = FontSizeChanged;
    public static RoutedEventHandler ArrowsComboboxLoaded = ArrowsComboboxLoadedHandler;
    public static SelectionChangedEventHandler ArrowsComboboxSelectionChanged = ArrowsComboboxSelectionChangedHandler;

    public static RoutedCommand Group = new RoutedCommand();
    public static RoutedCommand Ungroup = new RoutedCommand();
    //etc
    public static RoutedCommand Redo = new RoutedCommand();
    private readonly IDesignerCommandsReceiver _receiver;

    public ApplicationToolbar()
    {
        var designer = FindChild<DesignerCanvas>(Application.Current.MainWindow, "DesignerCanvas");
        _receiver = new DesignerCommandsReceiver(designer);
        AddCommandBindings();
        AddCommandGestures();
    }
    private void AddCommandBindings()
    {
        CommandBindings.Add(new CommandBinding(ApplicationCommands.New, New_Executed));

        // some more bindings

        CommandBindings.Add(new CommandBinding(Redo, Redo_Executed, Redo_Enabled));
    }
    private static void AddCommandGestures()
    {
        SelectAll.InputGestures.Add(new KeyGesture(Key.A, ModifierKeys.Control));
        Undo.InputGestures.Add(new KeyGesture(Key.Z, ModifierKeys.Control));
        Redo.InputGestures.Add(new KeyGesture(Key.Y, ModifierKeys.Control));
    }
    private void New_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        _receiver.New();
    }        

    // other commands implementations

    private void Redo_Enabled(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = StateSaver.Instance.IsRedoAvailable();
    }

    public static void ArrowsComboboxLoadedHandler(object sender, RoutedEventArgs e)
    {

    }
    private static void ArrowsComboboxSelectionChangedHandler(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
    {

    }

    // other event handlers     
}
public enum PathFinderType
{
    Straight,
    Ambient,
    Polygon
}

如何将此类绑定到xaml演示文稿?我尝试将ContentPresenter与指定的DataContext一起使用,但它不起作用:

<ContentPresenter Content="{StaticResource MyToolbar}" DataContext="{x:Type logic:ApplicationToolbar}"/>

更新 ApplicationToolbar.xaml现在是:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:logic="clr-namespace:DD.Logic;assembly=DD.Logic">
<ControlTemplate x:Key="MyToolbar">
    <ToolBar  Height="120">
//similar code
</ToolBar>
</ControlTemplate>

它打印出来:

<ContentPresenter>
        <ContentPresenter.Content>
            <logic:ApplicationToolbar />
        </ContentPresenter.Content>
        <ContentPresenter.ContentTemplate>
            <DataTemplate>
                <Control Template="{StaticResource MyToolbar}" />
            </DataTemplate>
        </ContentPresenter.ContentTemplate>
    </ContentPresenter>

仍然没有实现工作的命令&lt; - 通过将所有命令相关代码放回到设计器类来解决该问题。我想CommandBindings应该被添加到设计器对象的CommandBindingsCollection中,而不是应用程序工具栏。

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

<ContentPresenter>
    <ContentPresenter.Content>
        <logic:ApplicationToolbar />
    </ContentPresenter.Content>
    <ContentPresenter.ContentTemplate>
        <DataTemplate>
            <ContentControl Content="{StaticResource MyToolbar}" />
        </DataTemplate>
    </ContentPresenter.ContentTemplate>
</ContentPresenter>

Content的{​​{1}}属性应设置为DataContext / ViewModel的实例,ContentPresenter设置为ContentTemplate,定义此外观对象